欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

读写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();
}