源码整合时遇到问题了。能否指点一下。谢谢
这是自己写的一段获取当前目录文件名并且存入数组中的源码。
程序代码:
#include <stdlib.h> #include <windows.h> void Get_FileName() { system("dir /b *.csv > file_name.txt"); } int main() { Get_FileName(); int i=0; char file_name[255][255]; FILE *fp; fp=fopen("file_name.txt","r"); if(fp==NULL) { printf("\n error! can\'t find file_name.txt!"); getchar(); exit(0); } while(fscanf(fp,"%s",file_name[i])!=EOF) i++; fclose(fp); for(int j=0;j<i;j++) printf("%s\n",file_name[j]); system("pause"); return 0; }
这是一段学长写的可以单独处理一个固定名字和输出固定名字的CSV文件的源码
程序代码:
#include<fstream> #include<iostream> #include<sstream> #include<map> #include<string> #include<cstdlib> #include<windows.h> using namespace std; struct _data { string info; string name; }; class secTool { public: secTool() {}; ~secTool() {}; void handlefile() { cout << "请确保data.csv和dict.txt两个文件在本程序目录下" << endl; system("PAUSE"); system("CLS"); ifstream fData("data.csv"), fDict("dict.txt"); while (!fData) { cout << "data.csv不在程序目录下或者无法打开,正在重试!" << endl; fData.sync(); fData.clear(); Sleep(1000); system("CLS"); fData.open("data.csv", ios::_Nocreate); } while (!fDict) { cout << "dict.txt不在程序目录下或者无法打开,正在重试!" << endl; fDict.sync(); fDict.clear(); Sleep(1000); system("CLS"); fDict.open("dict.txt", ios::_Nocreate); } string temp, id, name; _data tData; getline(fData, temp); tableHead += temp + '\n'; getline(fData, temp); tableNum = temp.size() + 1; tableHead += temp + '\n'; getline(fData, temp); tableHead += temp + '\n'; while (fDict >> id >> name) { tData.name = name; dataMap.insert(make_pair(id, tData)); } fDict.close(); while (!fData.eof()) { getline(fData, temp); int i = temp.find(','); int j = temp.find(',', i + 1); string t = temp.substr(i + 1, j - i - 1); if (dataMap.count(t) > 0) { subNum++; stringstream stream; stream << subNum; dataMap[t].info = stream.str() + temp.substr(temp.find(','), temp.size() - temp.find(',')); } } fData.close(); } void outputfile() { map<string, _data>::iterator it; ofstream outFile("out.csv"); if (!outFile) { cout << "生成out.csv文件失败!" << endl; return; } outFile << tableHead; for (it = dataMap.begin(); it != dataMap.end(); it++) { if (it->second.info.size() == 0) { outFile << ++subNum << ',' << it->first << ',' << it->second.name; for (int i = 0; i < tableNum - 3; i++) outFile << ','; outFile << endl; } else outFile << it->second.info << endl; } cout << "输出文件out.csv成功!" << endl; outFile.close(); } private: map<string, _data> dataMap; string tableHead; int tableNum ; int subNum ; }; int main() { secTool sectool; sectool.handlefile(); sectool.outputfile(); system("PAUSE"); return 0; }
我现在想把它改成一个能够循环处理我数组里文件名的,并且输出到另一个文件夹里的工具,就是一次可以批量处理本目录下所有CSV。但是我还没有学c++,学长的代码看不懂。。改的不能够编译了。能不能帮我看一下,我哪里出问题了,谢谢。谢谢(*°∀°)=3
程序代码:
#include<fstream> #include<stdio.h> #include<iostream> #include<sstream> #include<map> #include<string> #include<cstdlib> #include<windows.h> using namespace std; struct _data { string info; string name; }; class secTool { public: secTool() {}; ~secTool() {}; void handlefile(char *name) { cout << "请确保data.csv和dict.txt两个文件在本程序目录下" << endl; system("PAUSE"); system("CLS"); ifstream fData("*name"), fDict("dict.txt"); while (!fData) { cout << "不在程序目录下或者无法打开,正在重试!" << endl; fData.sync(); fData.clear(); Sleep(1000); system("CLS"); fData.open("*name", ios::_Nocreate); } while (!fDict) { cout << "dict.txt不在程序目录下或者无法打开,正在重试!" << endl; fDict.sync(); fDict.clear(); Sleep(1000); system("CLS"); fDict.open("dict.txt", ios::_Nocreate); } string temp, id, name; _data tData; getline(fData, temp); tableHead += temp + '\n'; getline(fData, temp); tableNum = temp.size() + 1; tableHead += temp + '\n'; getline(fData, temp); tableHead += temp + '\n'; while (fDict >> id >> name) { tData.name = name; dataMap.insert(make_pair(id, tData)); } fDict.close(); while (!fData.eof()) { getline(fData, temp); int i = temp.find(','); int j = temp.find(',', i + 1); string t = temp.substr(i + 1, j - i - 1); if (dataMap.count(t) > 0) { subNum++; stringstream stream; stream << subNum; dataMap[t].info = stream.str() + temp.substr(temp.find(','), temp.size() - temp.find(',')); } } fData.close(); } void outputfile(char *name) { map<string, _data>::iterator it; ofstream outFile("out//*name"); if (!outFile) { cout << "生成out.csv文件失败!" << endl; return; } outFile << tableHead; for (it = dataMap.begin(); it != dataMap.end(); it++) { if (it->second.info.size() == 0) { outFile << ++subNum << ',' << it->first << ',' << it->second.name; for (int i = 0; i < tableNum - 3; i++) outFile << ','; outFile << endl; } else outFile << it->second.info << endl; } cout << "输出文件成功!" << endl; outFile.close(); } private: map<string, _data> dataMap; string tableHead; int tableNum ; int subNum ; }; void Get_FileName() { system("dir /b *.csv > file_name.txt"); //获取目录下的文件名 } int main() { Get_FileName(); int i=0; char file_name[255][255]; FILE *fp; fp=fopen("file_name.txt","r"); if(fp==NULL) { printf("\n error! can\'t find file_name.txt!"); getchar(); exit(0); } while(fscanf(fp,"%s",file_name[i])!=EOF) i++; i=0; fclose(fp); while(file_name[i]!='\0') { secTool sectool; sectool.handlefile(file_name[i]); sectool.outputfile(file_name[i]); i++; } system("PAUSE"); return 0; }