gpt4 book ai didi

c++ - std::filebuf VS2015 错误

转载 作者:行者123 更新时间:2023-11-28 00:08:22 29 4
gpt4 key购买 nike

这是代码:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
{
std::filebuf f;
f.open("test.txt", std::ios::in | std::ios::out | std::ios::trunc);
for (uint8_t ch = '0'; ch < '7'; ch++) {
f.sputc(ch);
}
}
{
std::filebuf f;
f.open("test.txt", std::ios::in | std::ios::out);
for (uint8_t ch = '0'; ch < '7'; ch++) {
f.sbumpc();
}
for (uint8_t ch = '7'; ch < '9'; ch++) {
f.sputc(ch);
}
}
{
std::ifstream f("test.txt");
std::string line;
std::getline(f, line);
std::cout << line << std::endl;
}

}

Windows 10
Microsoft (R) C/C++ 优化编译器版本 19.00.23026 per x64

cl /EHsc main.cpp && main.exe

输出是

0123456

代替:

012345678

我试图在 http://connect.microsoft.com/VisualStudio/feedback 上报告错误,但我得到了:

You are not authorized to submit the feedback for this connection.

有人知道我可以向其发送错误报告的电子邮件地址吗?

最佳答案

标准库文件流是根据 C FILE 定义的流:

[流媒体]/2

The restrictions on reading and writing a sequence controlled by an object of class basic_filebuf<charT, traits> are the same as for reading and writing with the Standard C library FILEs.

当您打开流进行读取 写入时,库的行为就好像它打开了一个FILE。处于更新模式(+)

enter image description here

从C标准我们可以看出

7.21.5.3/7

When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

即你不能在不查找的情况下跟随读取和写入,除非你已经到达文件末尾,你没有在这里,你只读取并包括最后一个字符。

在写入之前,您要么必须寻找放置位置,要么最后一次从流中读取以达到结尾,例如

f.pubseekoff(0, std::ios_base::cur);

然而,以上不适用于最新的 VC++ 库,这实际上可能是一个错误。寻找结束或开始按预期工作。

关于c++ - std::filebuf VS2015 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246523/

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