1

I've been doing some basic file manipulations with iostreams in Visual Studio 2015 and noticed this weird behavior.

#include <fstream>
#include <iostream>
#include <sstream>

void main(int argc, char** argv)
{
    std::wifstream stream("example.txt");
    //std::ifstream stream("example.txt");
    //std::wstringstream stream(L"abcdefghijklmnopqrstuvxyz");

    std::wcout << (char)stream.peek() << "\n"; // a
    stream.tellg();
    std::wcout << (char)stream.peek() << "\n"; // b
    stream.tellg();
    std::wcout << (char)stream.peek() << "\n"; // c
    stream.tellg();
    std::wcout << (char)stream.peek() << "\n"; // d
    stream.tellg();
    std::wcout << (char)stream.peek() << "\n"; // e
    stream.tellg();
    std::wcout << (char)stream.peek() << "\n"; // f
    stream.tellg();
    std::wcout << (char)stream.peek() << "\n"; // g
    stream.tellg();
}

example.txt contains:

abcdefghijklmnopqrstuvxyz

It seems like stream.tellg() advances the stream's current character by one. This happens only for std::wifstream. std::ifstream and std::wstringstream seem not to be affected.

Is this a bug in visual studio stl implementation?

unlink
  • 15
  • 3

1 Answers1

0

This issue is similar with wifstream/wfstream implementation bug in Visual C++ 2010.

As Stephan said:

C++ Standard does not provide behavior guarantees when calling tellg() on a file opened in text mode.

I guess that's the cause.

wangke1020
  • 239
  • 1
  • 2
  • 7