windows读取文件夹下所有的文件——C++

                intptr_t hFile = 0;
                struct _finddata_t fileInfo;
                std::string pathName, exdName;
                if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -1) {
                    return;
                }
                do {
                    std::cout << fileInfo.name << std::endl;

                    // read the file 
                    if (strcmp(fileInfo.name, ".") != 0 && strcmp(fileInfo.name, "..") != 0) {
                        std::ifstream file(path + "\\" + fileInfo.name);
                        std::string eachRow;
                        std::stringstream stringStream;
                        if (file) {
                            while (!file.eof()) {
                                std::getline(file, eachRow);
                                //std::cout << eachRow << std::endl;

                                if (!eachRow.empty()) {
                                    // replace "," with blank
                                    for (int i = 0; i < eachRow.length(); i++) {
                                        if (eachRow[i] == ',') {
                                            eachRow[i] = ' ';
                                        }
                                    }
                                    //std::cout << eachRow << std::endl;

                                    // get every word from a row 
                                    std::string updateEachRecord = "insert into _";
                                    updateEachRecord.append(date);
                                    updateEachRecord.append("(InstrumentID,TradeDate,UpdateTime,UpdateMillisec,LastPrice,Volume,BidPrice1,BidVolume1,AskPrice1,AskVolume1,OpenInterest,Turnover) value ('");

                                    std::string tempWord;
                                    stringStream.str(eachRow);

                                    stringStream >> tempWord;
                                    updateEachRecord.append(tempWord);
                                    updateEachRecord.append("',");
                                    updateEachRecord.append(date);
                                    while (stringStream >> tempWord) {
                                        updateEachRecord.append(",'");
                                        updateEachRecord.append(tempWord);
                                        updateEachRecord.append("'");
                                    }
                                    updateEachRecord.append(")");
                                    std::cout << updateEachRecord << std::endl;
                                    stringStream.clear();

                                    // update the record to mysql;
                                    num = mysql_query(&mysql2, const_cast<char *>(updateEachRecord.c_str()));
                                    if (num == 0) {
                                        std::cout << "Succeed to update the record to mysql!" << std::endl;
                                        su++;
                                    }
                                    else {
                                        std::cout << "Fail to update the record to mysql! Error code = " << num << std::endl;
                                        getchar();
                                        fa++;
                                    }
                                }
                            }
                        }
                        else {
                            std::cout << "The file " << fileInfo.name << " is empty!" << std::endl;
                        }
                    }

                } while (_findnext(hFile, &fileInfo) == 0);
                _findclose(hFile);

注意头文件为io.h,首先取出来的两个文件的名字分别为.和..,表示当前目录和上一层目录,需要剔除掉这两种情况。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值