繁体   English   中英

在c ++中搜索特定行后如何将文本附加到文件末尾?

[英]How to append a text to the end of the file after searching for specific line in c++?

我有一个文件,我必须从中找到第一个单词和第二个单词,通过逐行搜索,然后将文本(变量 list_of_names)附加到我们找到的行的末尾。 我有以下代码,但它将文本附加到文件末尾,而不是在特定行上

    string line;
    string courseid, course_section;
    cout<<"What is a courseID: ";
    cin>>courseid;
    cout<<"What is the  course section: ";
    cin>>course_section;

    string first;
    ofstream outfile;
    outfile.open("course_students.csv", ios_base::app);
    ifstream file;
    file.open("course_students.csv");
    
    if(outfile.is_open()){ 
                while(getline(file, line)) { 
                    stringstream sst(line); 
                    string section;

                    getline(getline (sst,first,','),section, ',');
                    //cout<<first<<endl;
                    if (courseid==first && course_section==section) {
                        //outfile<<save1;
                        outfile<<list_of_names;
                    }
            }
}

outfile.close();
}

我的csv如下:

CSCI-UA.0465,1,
CSCI-UA.0473,1,
CSCI-UA.0470,1,
CSCI-UA.0453,1,
CSCI-UA.0421,1,
CSCI-UA.0310,1,
CSCI-UA.0201,1,
CSCI-UA.0102,5,
CSCI-UA.0101,2,
CSCI-UA.0101,1,
CSCI-UA.0061,1,
CSCI-UA.0060,1,
CSCI-UA.0004,4,
CSCI-UA.0004,1,
CSCI-UA.0002,3,
CSCI-UA.0002,2,
CSCI-GA.3033,13,
CSCI-GA.3033,10,
CSCI-GA.3033,2,
CSCI-GA.3033,1,

任何帮助将不胜感激!

下面在输出( course_students_output.csv )上创建一个新文件,您可以在之后重命名。

    string line;
    string courseid, course_section;
    cout<<"What is a courseID: ";
    cin>>courseid;
    cout<<"What is the  course section: ";
    cin>>course_section;

    //string first;
    ifstream file("course_students.csv");
    ofstream outfile("course_students_output.csv");

    if ( file.is_open() ) {
        while ( getline(file, line) ) {
            stringstream sst(line, ios_base::in | ios_base::app | ios_base::out);
            outfile << sst.str();
            
            string first, section;
            getline(getline(sst,first,','), section, ',');
            if (courseid==first && course_section==section) {
                outfile << " " << list_of_names;
            }
            outfile << '\n';
        }
    }

    file.close();
    outfile.close();
}

如果您还希望在文件末尾重复附加list_of_names的行,请在条件主体内添加一个新的stringstream对象,如下所示:

    // ... previous code from above
    if ( file.is_open() ) {
        stringstream end_sst(ios_base::in | ios_base::app | ios_base::out);
        while ( getline(file, line) ) {
            stringstream sst(line, ios_base::in | ios_base::app | ios_base::out);
            outfile << sst.str();

            string first, section;
            getline(getline(sst,first,','), section, ',');
            if (courseid==first && course_section==section) {
                end_sst << line << " " << list_of_names << '\n';
                outfile << " " << list_of_names;
            }
            outfile << '\n';
        }
        outfile << end_sst.str();
    }
    // add end lines from above...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM