I'm reading different .csv files from different sources. I am not creating those files.
At some point when reading these files I need to skip a few lines and use this:
void skipLines(unsigned int nLines, std::ifstream& file){
char l[1024];
for (unsigned int i = 0; i < nLines; i++)
file.getline(l, 1024);
}
This worked until now for a certain file.
The file open correctly, then I want to skip the first 5 line and I run skipLines(5, file)
.
By debugging, I can see that l
correctly gets the data from the first 4 lines. When reaching the next line (which should have text), it just shows in the debugger as ""
.
If I check the characters which are found in the that line I see the first character is '\0'
(end of string?), the following characters are the characters that "should" be from the previous line.
If continue to run skipLines()
on more lines, it keeps looping on the same data. Always l
with the same set of characters starting with '\0'
.
What can cause this? And how should I correctly "skip" lines when parsing a text file?
EDIT: If the content of the file is copied and pasted to new file and saved. The problem is gone. But that is not a work around I can use in this case.
I don't want to share the exact content of the file, but it basically looks like this:
Transfers
User,****,*****
Timestamp,Type,***,Subtotal,Fees,Total,Currency,Price,Payment Method,ID,Share
2017-06-01 10:54:42 -0700,***,0.25638772,50.0,2.00,52.0,***,195.02,***************,***54e27dd1741d3810a8e0,0
The line starting with Timestamp is where it loops.