0

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.

remi
  • 772
  • 2
  • 11
  • 31
  • Can you post the offending file, or a reproduction of it? ([mcve]) – AndyG Jan 26 '18 at 18:53
  • Perhaps the line is empty? How many lines are there in the file? It is a *text* file? Opened in text mode? Can you perhaps show us the input file you use (or at least the six first lines of it)? – Some programmer dude Jan 26 '18 at 18:54
  • https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction –  Jan 26 '18 at 18:55
  • If I copy paste the content of the file to a new file, save it, then read it, the problem is gone. I prefer to not share the original file. If the file is opened in an editor, it is nothing but comma separated text. – remi Jan 26 '18 at 18:56
  • Perhaps a line-ending problem? If you look at the file in a hex-editor, are the newlines what you expect them to be? – Some programmer dude Jan 26 '18 at 19:00
  • @Someprogrammerdude opening in Notepad++ shows the correct lines at least.. – remi Jan 26 '18 at 19:03
  • @TheDude any insight in how would apply that overload in this case? – remi Jan 26 '18 at 19:07
  • 1
    @remi000 Be sure to `ignore()` any line ending characters before applying `std::getline()`,` Also using a fixed buffer doesn't look like a good idea for my taste, rather use a `std::string` variable. –  Jan 26 '18 at 19:11
  • @TheDude Ok, thanks. So now, just saving by that text file once, the problem was gone. So its some sort of "hidden" characters that cause this? That the text-editor maybe ignores/cancel? – remi Jan 26 '18 at 19:16
  • @remi000 Did you actually read the linked Q&A I've been pointing you to? –  Jan 26 '18 at 19:19
  • @TheDude I will have a deeper dive in it. – remi Jan 26 '18 at 19:20
  • Perhaps `getline` fails and isn't actually modifying your buffer, which still contains the last read line. You should do some error handling. – rustyx Jan 26 '18 at 19:45

0 Answers0