简体   繁体   中英

fstream get method not behaving consistently

I'm trying to read a file one character at a time, but for some reason the get pointers' position isn't being reported correctly:

std::wifstream stream(L"tokenizer.txt");

int pos = static_cast<int> (stream.tellg());

stream.get();
stream.get();

pos = static_cast<int> (stream.tellg());

stream.close();

The value of pos before the two calls to stream.get is 0, as it should be, but only 1 after them. The strange thing is that any calls to get after the first all affect the result of stream.tellg. In other words, the first call to get will not cause a change in result from tellg.

If I call unget after the first call to get, it still reports the position as being 0, but it treats it as being at position 1, as the next 2 characters are '/' and 'c', while they should be '/' '/'.

The contents of the file are as follows:

//comment
123.45 100 "delimited string" str ing

(The space between str and ing is intentional.)

The file itself is saved in ANSI using Notepad++. I'm using Visual Studio 2010 Ultimate SP1.

When compiled with G++ 4.7.0 on Mac OS X 10.7.3 (that's a 'home built' compiler), then the following code, closely derived from your outline, produces the output:

pos = 0
c = 47
c = 47
pos = 2

which is what you'd expect.

Compilation command line:

g++ -O3 -g   -Wall -Wextra  xx.cpp -o xx  

Actual code:

#include <iostream>
#include <fstream>

int main(void)
{
    std::wifstream stream("tokenizer.txt");

    int pos = static_cast<int> (stream.tellg());
    std::cout << "pos = " << pos << std::endl;

    int c = stream.get();
    std::cout << "c = " << c << std::endl;

    c = stream.get();
    std::cout << "c = " << c << std::endl;

    pos = static_cast<int> (stream.tellg());
    std::cout << "pos = " << pos << std::endl;

    stream.close();
}

Apart from adding diagnostic printing, headers and the main() function, the only significant difference is that there was no constructor for std::wifstream that accepts an array of wchar_t , so I dropped the L .

This suggests that there is something odd about either the library you're linking with or the compiler you're using. Could it be optimizing out one of the two calls to get() ? Are you sure there isn't a //...comment ending in backslash\\ on the line before one of the two calls to get() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM