Tellg() + "\r\n" (windows newline) malfunction. (.csv file)

Hi everybody,

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <string.h>

using namespace 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...

Thanks in advance!
Last edited on
Microsoft operating systems use two characters as an end of line marker, while POSIX systems use one character.

This code in Linux (Kdevelop--Cmake) , with a linux .csv, works as expected.
I'm not sure what you expected.
I'm not sure what you expected.

I expected a correct output:

0blabla,blabla
14blabla,blabla
27blabla,blabla
....

with windows I'm getting:

0blabla,blabla
23la,bla
45a
53labla
....

(I've made up the numbers, just to make the example).
Last edited on
seek/tell functions are for files opened in binary mode, not for text mode. Add ios::binary to your fstream (which should be ifstream) constructor.

And while you're at it, fix the error in the first loop, it should be n=0; while (getline (csv, line))...

Edit: technically, tellg should work, since zero-offset seeks ate valid in text mode.. Do try binary, though.
Last edited on
Ok, now I get it! I can use binary mode to use tellg() and still use getline, etc.

Thanks Cubbi!

As I see in this post:
http://www.cplusplus.com/forum/beginner/4651/

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.

Summing up:
fstream csv ("test_file.csv", ios::in | ios::out | ios::binary);
solves my problem.

Thank you all!
Last edited on
Topic archived. No new replies allowed.