这是代码:
#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每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.
有人知道我可以编写的电子邮件地址来报告错误吗?
答案 0 :(得分:3)
标准库文件流是根据C FILE
流定义的:
[fstreams] / 2
读取和写入由...控制的序列的限制 类
basic_filebuf<charT, traits>
的对象与for相同 使用标准C库FILE
进行读写。
当您打开用于阅读和写入的流时,该库就像在更新模式下打开FILE
一样(+
)
从C标准我们可以看到
7.21.5.3 / 7
以更新模式打开文件时(
'+'
作为第二个或第三个) 上面的模式参数值列表中的字符),输入和 可以在关联的流上执行输出。但是,输出 在没有干预呼叫的情况下,不得直接输入fflush
函数或文件定位函数(fseek
,fsetpos
或rewind
)和输入不得直接跟随 输出没有中间调用文件定位功能, 除非输入操作遇到文件结尾。
即。你不能在没有寻求的情况下通过写作来阅读,除非你已经到了文件的末尾,你没有在这里,你只读过并包括最后一个字符。
在写作之前,你要么必须寻找放置位置,要么最后一次从流中读取结束,例如。
f.pubseekoff(0, std::ios_base::cur);
上面的内容不适用于最新的VC ++库,这实际上可能是一个bug。寻求结束或开始按预期工作。