gpt4 book ai didi

c++ - 使用相同的 fstream 读取和写入相同的文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:03 25 4
gpt4 key购买 nike

我有一个文件已经包含一些数据(比如 8 kB)。我想从文件的开头读取一些内容,然后从我完成读取的地方开始覆盖数据。所以我尝试使用以下代码:

std::fstream stream("filename", std::ios::in | std::ios::out | std::ios::binary);

char byte;
stream.read(&byte, 1);

// stream.seekp(1);

int bytesCount = 4096;

auto bytesVec = std::vector<char>(bytesCount, 'c');
char* bytes = bytesVec.data();

std::cout << stream.bad() << std::endl;

stream.write(bytes, bytesCount);

std::cout << stream.bad() << std::endl;

如果我执行此代码,第一个 bad() 返回 false,但第二个返回 true 并且实际上没有写入任何内容。

如果我将 bytesCount 减少到任何小于 4096 的值(大概是某个内部缓冲区的大小),第二个 bad() 返回 false , 但仍然没有任何内容被写入。

如果我取消注释 seekp() 行,写入开始工作:bad() 返回 false 并且字节实际被写入。

为什么这里需要seekp()?为什么没有它就不能工作? seekp() 是执行此操作的正确方法吗?

我在 Windows 7 上使用 Visual Studio 2012。

最佳答案

您违反了混合读取和读取的限制对以 MS 的 fstream 更新模式打开的文件进行写操作库继承自其 C <stdio.h>实现。

C 标准(我引用的是 C99,但在这一点上与 C89 没有区别)在 7.19.5.3/6 状态:

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.

(我的重点)。

所以你的 stream.seekp(1)解决方案,这将转化为 C fseek , 是正确的。

GNU C 库没有此标准限制,因此您发布的代码有效正如使用 GCC 构建时预期的那样。

女士 <fstream>库在继承方面符合 C++ 标准这个限制。 fstream s 是使用 basic_filebuf<charT,traits> 实现的.在 (C++11) 标准对该模板的说明中,在 § 27.9.1.1/2,它只是说:

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

关于c++ - 使用相同的 fstream 读取和写入相同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53705543/

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