I've already posted in the beginners forum a problem I was facing, and after all, I've realized there seems to be a heavy problem (maybe a bug?) with using tellg () with windows .csv files (with Linux .csv files it does work as excepted, though). Let me copy part of the post and see if anyone here knows what's going on:
Let's take this example code:
Considering that you have a testing file (test_file.csv) with some rows, and two columns (that's what I'm using) filled:
#include <iostream>
#include <fstream>
#include <string.h>
usingnamespace std;
int main(int argc, char *argv []) {
fstream csv ("test_file.csv");
string line;
int i, n;
n=-1; //.csv file always end with an empty line, that's why n=-1
while (! csv.eof ())
{
n++;
getline (csv, line);
}
csv.clear();
csv.seekg (0);
csv.seekp (0);
for (i=0; i<n; i++)
{
cout << csv.tellg ();
getline (csv, line);
cout <<line <<endl; //it outputs the first line correctly, but not the others.
}
return 0;
}
This code in Linux (Kdevelop--Cmake) , with a linux .csv, works as expected.
In windows (Code::Blocks--GNU GCC) whit a windows .csv, prints out the lines wrongly, as well as the number that tellg() should point to. However when you comment the cout <<csv.tellg(); it does work as expected.
Also, In windows with a Linux .csv, it does work as expected!
Summing up, it seems like a problem with tellg() and \r\n newlines (windows .csv)... ¿? My confusion couldn't be bigger...
the only difference between binary and text is that binary mode won't translate \r\n into \n, thus I must manually erase the \r from the string. (if not, when I print out line, it will be overwritten for the next output, correct?)
anyway, I would prefer to be sincere... actually, seek/tell are indeed functions for text opening mode(and so says the documentation) it's just that c++ has a shameful bug in it when handling CRLF files.