2

I am trying to read from file using fstream .The file I am trying to read has this content:

1200 1000 980 890 760 

My code:

#include <fstream> #include <iostream> using namespace std; int main () { fstream file("highscores.txt", ios::in | ios::out); if (!file.is_open()) { cout << "Could not open file!" << endl; return 0; } int cur_score; while (!file.eof()) { file >> cur_score; cout << file.tellg() << endl; } } 

The output is:

9 14 18 22 26 

Why after first read the tellg() returns 9, the first read is the number (1200) which is 4 positions and I know there is \r and \n so this make 6 positions. Also. if I add more number in my file tellg() will return a bigger number after first read.

9
  • 2
    Don't use while(!file.eof()). Commented May 30, 2015 at 12:53
  • @πάνταῥεῖ: Exactly. Note that the OP has five input data, but six lines of output. Tsk Tsk. Commented May 30, 2015 at 12:57
  • There is no other white space, I made sure that after each number there are no spaces...I don't know if this info help but if you make it as csv it will work fine...the result of tellg() will be making more sense Commented May 30, 2015 at 12:59
  • 1
    Cannot reproduce, voting to close. Commented May 30, 2015 at 13:02
  • 1
    @KerrekSB the problem can be reproduced compiling with MingW under windows, and reading a CRLF file opened in text mode. Commented May 30, 2015 at 15:27

1 Answer 1

2

If you've saved your file in UTF8 with a text editor, there might be an UTF8 BOM at the beginning of the file. This BOM is 3 chars long, so added to the 6, it would make 9.

If you want to be sure, check out the beginning of the file, with:

fstream file("highscores.txt", ios::in | ios::out | ios::binary); if(file) { char verify[16]; file.read(verify, sizeof(verify)); int rd = file.gcount(); for(int i = 0; i<rd; i++) { cout << hex << setw(2) << (int)verify[i] << " "; } cout <<dec << endl; } 

Edit:

Running on windows with MSVC2013 on the file and I found 4, 10, 15, 20, 25 as expected, and I couldn't reproduce your figures.

I've now done a test with mingw and here I get exactly your numbers, and the strange effect that increasing the number of lines increases the output.

THIS IS A BUG of MINGW when you read your windows (CRLF line separator) file in text mode:

  • If I save the file in UNIX style (i.e. LF line separator), I get with the same programme 4,9,13,17 which is again the expected value for a linux system.

  • If I save the file in WINDOWS style (i.e. CRLF line separator), and if I change the code to open the file in ios::binary, I get the awaited 4,10,15,20,25.

Apparently it's an old problem.

Sign up to request clarification or add additional context in comments.

11 Comments

but what about the dependency of the tellg() result on the number of lines in the txt file...I mean if I add one more number to the txt file I will get 10 instead of 9?
do you run exactly the code that you've posted in the question ? On which OS do you do this ? And what are the results if you use my verifying code above ?
@ehabibrahim do you add the line to the file with a text editor ? Wich one ?
I use win7 and run the exact code...the result of your code is this: 31 32 30 30 d a 31 30 30 30 d a 39 38 30 d
Ok ! I've reproduced it now, using migw under windows.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.