简体   繁体   English

std :: filebuf VS2015错误

[英]std::filebuf VS2015 bug

This is the code: 这是代码:

#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 Windows 10
Microsoft (R) C/C++ Optimizing Compiler versione 19.00.23026 per x64 每个x64的Microsoft(R)C / C ++优化编译器版本19.00.23026

cl /EHsc main.cpp && main.exe

and the output is 输出是

0123456

instead of: 代替:

012345678

I tried to report the bug on http://connect.microsoft.com/VisualStudio/feedback but i got: 我试图在http://connect.microsoft.com/VisualStudio/feedback上报告错误,但是我得到了:

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

Does someone knows an email address to which I can write to report the bug? 有人知道我可以写该错误报告的电子邮件地址吗?

The standard library file streams are defined in terms of C FILE streams: 标准库文件流是根据C FILE流定义的:

[fstreams] / 2 [fstreams] / 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 FILE s. 读写由类basic_filebuf<charT, traits>的对象控制的序列的限制与使用标准C库FILE进行读写的限制相同。

When you open a stream for reading and writing, the library acts as though it opened a FILE in update mode ( + ) 当您打开用于读取写入的流时,库的行为就像在更新模式( + )中打开了FILE

在此处输入图片说明

From the C standard we can see that 从C标准我们可以看到

7.21.5.3 / 7 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. 但是,在没有中间调用fflush函数或文件定位函数( fseekfsetposrewind )的情况下, 输出之后不能直接输入,在没有中间调用文件位置的情况下输入之后不能直接输出函数,除非输入操作遇到文件结尾。

Ie you cannot follow a read with a write without seeking, unless you have hit the end of file, which you haven't here, you have only read up to and including the final character. 也就是说,除非找到文件末尾(除非您没有在此处找到文件末尾),否则您不能不读就不能进行写入操作,您只能读到并包括最后一个字符。

Before writing, you will either have to seek the put position, or read from the stream one last time to hit the end, eg 在写之前,您要么必须寻找放置位置,要么最后一次从流中读取以达到终点,例如

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

The above doesn't work with the latest VC++ library however, and this might actually be a bug. 上面的方法不适用于最新的VC ++库,但这实际上可能是一个错误。 Seeking to the end or beginning works as expected. 寻求结束或开始按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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