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

c++操作文件读写txt文本

程序员文章站 2022-03-18 23:37:41
/**********************************读取第一列数据***********************************/void ReadTxt(){ char buffer[256]; ifstream inFile("FilePath\\1.txt",ios::in); if(!inFile.is_open()) { return; } vector all....
/***************************
*******读取第一列数据********
***************************/
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();
}

本文地址:https://blog.csdn.net/m0_37709009/article/details/107911528