简体   繁体   中英

How to extract specific substring from getline function in C++?

I'm fairly new to C++ so please forgive me if my terminology or methodology isn't correct.

I'm trying to write a simple program that:

  1. Opens two input files ("infileicd" and "infilesel").
  2. Opens a single output file "list.txt".
  3. Compares "infilesel" to "infileicd" line by line.
  4. If a line from "infilesel" is found in "infileicd", it writes that line from "infileicd" to "list.txt", effectively making a separate log file.

I am using the getline() function to do this but have run into trouble when trying to compare each file line. I think it might be easier if I could use only the substring of interest to use as a comparison. The problem is that there are multiple words within the entire getline string and I am only really interested in the second one. Here are two examples:

"1529 nic1_mau_op_mode_3 "8664afm007-01" "1" OUTPUT 1 0 LOGICAL 4 4136"

"1523 pilot_mfd_only_sel "8664afm003-02" "1" OUTPUT 1 0 LOGICAL 4 4112"

"nic1_mau_op_mode_3" and "pilot_mfd_only_sel" are the only substrings of interest.

It would make it a lot easier if I could only use that second substring to compare but I don't know how to extract it specifically from the getline() function. I haven't found anything suggesting it is impossible to do this, but if it is impossible, what would be an alternative method for extracting that substring?

This is a personal project so I'm under no time contstraints.

Any assistance is greatly apprecated in advance. Here is my code (so far):

int main()
{
    //Open the file to write the selected variables to.
    ofstream writer("list.txt");

    //Open the selected variabels file to be read.
    ifstream infilesel;
    infilesel.open("varsel.txt");

    //Open the icd file to be read.
    ifstream infileicd;
    infileicd.open("aic_fdk_host.txt");

    //Check icd file for errors.
    if (infileicd.fail()){
        cerr << "Error opening icd.\n" << endl;
        return 1;
    }
    else {
        cout << "The icd file has been opened.\n";
    }

    //Check selected variables file for errors.
    if (infilesel.fail()){
        cerr << "Error opening selection file.\n" << endl;
        return 1;
    }
    else {
        cout << "The selection file has been opened.\n";
    }

    //Read each infile and copy contents of icd file to the list file.

    string namesel;
    string nameicd;

    while(!infileicd.eof()){ 

        getline(infileicd, nameicd);
        getline(infilesel, namesel);

        if (nameicd != namesel){ //This is where I would like to extract and compare the two specific strings
            infileicd; //Skip to next line if not the same

        } else {
                writer << nameicd << namesel << endl;
        } 
    }


    writer.close();
    infilesel.close();
    infileicd.close();

    return 0;
}

So, based on what we discussed in the comments, you just need to toss the stuff you don't want. So try this:

string namesel;
string nameicd;
string junk;

while(!infileicd.eof()){ 

    // Get the first section, which we'll ignore
    getline(infileicd, junk, ' ');
    getline(infilesel, junk, ' ');

    // Get the real data
    getline(infileicd, nameicd, ' ');
    getline(infilesel, namesel, ' ');

    // Get the rest of the line, which we'll ignore
    getline(infileicd, junk);
    getline(infilesel, junk);

Basically, getline takes a delimiter, which by default is a newline. By setting it as a space the first time, you get rid of the first junk section, using the same method, you get the part you want, and then the final portion goes to the end of the line, also ignoring it.

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