Search code examples
c++getlineofstream

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

}

Solution

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