读写txt文本
程序员文章站
2022-06-11 14:30:23
...
/***************************
*******读取第一列数据********
***************************/
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();
}
上一篇: 用这个给宝宝洗头智力会下降 千万要警惕
下一篇: php常用的工具开发整理