1

ANSWER

Even though everything was compiled and run on windows, I completely forgot tellg behaves as an unformatted input function and cannot be used reliably in text mode which is why I see the discrepancy. See std::basic_istream::tellg for details.

ORIGINAL

The following extremely simple program is giving me different results with gcc 4.7.1 (mingw) and MSVC2012:

#include <iostream>
#include <fstream>
int main()
{
    std::ifstream in("test.txt");
    int i;
    in >> i;
    std::cout << in.tellg() << std::endl;
}  

The test.txt is as follows (IMPORTANT NOTE: There is a newline after the 1)

1

MSVC Output:

1

gcc 4.7.1 (mingw) Output:

2

Question

I believe gcc is correct since operator>> should extract the newline, but am not certain. Which compiler is correct?

(Note: Both programs were compiled and run on windows).

HEX of text file

31 OD OA

If I create a linux-like line ended file, i.e. 31 0A, MSVC outputs 0 and gcc outputs 1.

4

1 回答 1

2

Following my comment, Please refer to this excellent post Why does my program produce different results on Windows and Linux, about file reading with ifstream?

于 2013-03-30T22:02:38.710 回答