简体   繁体   中英

getline(file,line) is skipping lines - C++

I am taking contents from one file and transferring them to another. In particular, I am looking for lines that contain the substring "LIBOR/Swap". However, this is irrelevant and I've commented them out. So, this particular code at the moment really takes every line in one .csv file (file) and transfers them to another .csv file (temp).

The issue is that the transferring skips lines. See attached photos to see the lines that it's skipping in the transfer of data.

original file

new file

As you can see, with the first USD LIBOR/Swap line, it skips the 1 Day rate. This happens elsewhere throughout the code.

Any reason as to why? Thanks.

ifstream file;
ofstream temp;

file.open("YC Rate Levels.csv");
temp.open("Temp.csv");

if (file.is_open())
{
    string line;
    string test = "LIBOR/Swap";

    while (getline(file, line))
    {
          getline(file, line);
        //if(line.find(test) != string::npos)
        //{
            temp << line << endl;
        //}
    }

    temp.close();
    file.close();

} else cout << "File did not open.";

std::cin.clear();
std::cin.ignore(32767, '\n'); 
std::cin.get(); 

}

You are calling getline(file, line) twice - once inside the while() header and then again inside the body. Remove the second one.

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