gpt4 book ai didi

c++ - wifstream.tellg() 在流中的前进位置?

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:12 30 4
gpt4 key购买 nike

我一直在 Visual Studio 2015 中使用 iostream 进行一些基本的文件操作,并注意到这种奇怪的行为。

#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 包含:

abcdefghijklmnopqrstuvxyz

似乎 stream.tellg() 将流的当前字符前进了一个。这仅发生在 std::wifstream 中。 std::ifstreamstd::wstringstream 似乎没有受到影响。

这是 visual studio STL 实现中的错误吗?

最佳答案

此问题与 wifstream/wfstream implementation bug in Visual C++ 2010 类似.

正如斯蒂芬所说:

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

我想这就是原因。

关于c++ - wifstream.tellg() 在流中的前进位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191302/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com