/*************************** *******读取第一列数据******** ***************************/ void ReadTxt() { char buffer[256]; ifstream inFile("FilePath\1.txt",ios::in); if(!inFile.is_open()) { return; } vector<string> allData; //vector<string> firstColList; vector<int> firstColList;//第一列数据(int型) while(!inFile,eof) { inFile.getline(buffer,300);//逐行读取 char *allStr = strtok(buffer,","); while(allStr != NULL) { string str(allStr); allData.push_back(str); allStr = strok(NULL,","); } if(allData.Size != 2)//每行两列数据 continue; firstColList.push_back(atoi(allData[0].c_str()); } inFile.close(); } /*************************** **********写入数据*********** ***************************/ void WriteTxt(int id,string name) { ofstrean outFile; outFile.open("FilePath\1.txt", ios::out | ios::app); string inputID = to_string(id); if(!outFile.is_open()) return; outFile << inputID << ","<< name <<endl; outFile.close(); }
c/c++开发分享c++操作文件读写txt文本地址:https://blog.csdn.net/m0_37709009/article/details/107911528
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/598138.html