gpt4 book ai didi

c++ - 为什么我在使用 fwrite() 和 fread() 时遇到问题?

转载 作者:行者123 更新时间:2023-12-02 10:16:36 25 4
gpt4 key购买 nike

这是我的程序:

#include <iostream>

int main()
{
// open file1.txt
FILE* file1;
fopen_s(&file1, "file1.txt", "wb+");

// write array of 5 integers to file.txt
int array1[5] { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
fwrite(array1, sizeof(array1[0]), 5, file1);
}

fseek(file1, 0, SEEK_SET);
int tempValue;
fread(&tempValue, sizeof(tempValue), 1, file1);
// fseek(file1, 0, SEEK_CUR);
fwrite(&tempValue, sizeof(tempValue), 1, file1);
}

在运行时,程序崩溃并显示以下信息:
>     Expression ("Flush between consecutive read and write.",                 
> !stream.has_any_of(_IOREAD))

但是如果我取消注释 fseek(file1, 0, SEEK_CUR);考虑到文件指针没有被移动,一切都会好起来的。那为什么呢?
我使用 Visual Studio 2019

附言
为什么这很好用?
#include <iostream>

int main()
{
FILE* file1;
fopen_s(&file1, "data.txt", "wb+");
int value = 7;
fwrite(&value, sizeof(value), 1, file1);
fwrite(&value, sizeof(value), 1, file1);

fseek(file1, 0, SEEK_CUR);
fwrite(&value, sizeof(value), 1, file1);
fread(&value, sizeof(value), 1, file1);
}

最佳答案

读到写

来自 Microsofts C Runtime Library documentation

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed. (The file is said to be open for "update".) However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file-positioning function. The file-positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file-positioning function.



在读/写操作之间进行更改需要文件位置功能,
在您的代码片段中,您有:
...
fseek(file1, 0, SEEK_SET);
int tempValue;
fread(&tempValue, sizeof(tempValue), 1, file1);
// fseek(file1, 0, SEEK_CUR);
fwrite(&tempValue, sizeof(tempValue), 1, file1);
...

由于您正在从读取更改为写入,因此您需要调用文件位置函数(fsetpos、fseek 或 rewind)。

写读

至于从写入到读取,您仍然需要调用文件位置函数。然而,要回答为什么第二个代码块有效,我们需要知道 fwrite() 在成功时做了什么。

根据 same Microsoft documentation ,

... The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written.



考虑您提供的代码:
...
FILE* file1;
fopen_s(&file1, "data.txt", "wb+");
int value = 7;
fwrite(&value, sizeof(value), 1, file1);
fwrite(&value, sizeof(value), 1, file1);

fseek(file1, 0, SEEK_CUR);
fwrite(&value, sizeof(value), 1, file1);
fread(&value, sizeof(value), 1, file1);
...

假设所有 fwrite() 都成功,您的文件指针将位于 EOF。
Since the input operation encounters EOF ,代码块将执行得很好。

但是,如果 fwrite 失败,您应该遵循指南并调用 fsetpos、fseek 或 rewind。

类似的 Stackoverflow 问题

C standard library corner case

Why is fseek or fflush always required between reading and writing in the update modes?

关于c++ - 为什么我在使用 fwrite() 和 fread() 时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61702763/

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