简体   繁体   中英

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

I have a file from which i have to find get the first word and the second word in line, by searching from line to line, then append a text( variable list_of_names) to the end of the line that we found. I have the following code, but it appends a text to the end of the file, and not on the specific line

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

My csv below:

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,

Any help will be appreciated!

This below creates a new file on output ( course_students_output.csv ) that you can rename afterwards.

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

If you also want the lines with appended list_of_names to repeat at the end of the file, add a new stringstream object inside the body of the conditional, this way:

    // ... 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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