从未使用过 fwrite(),所以我不确定我做错了什么。我现在只是在测试它,我想做的就是尝试将单个字符写入文件,直到它到达末尾。我正在写的文件是我从老师的网站上下载的。当我检查它的属性时,该类型仅列为“文件”。它应该只是一个 2MB 的空文件,供我们将代码写入(如果有人想知道,文件系统实验室)。这是我的代码:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
FILE *fp;
char testing[2] = {'c'};
fp = fopen("Drive2MB", "rw");
fseek(fp, 0, SEEK_SET); //make sure pointers at beginning of file
while((c=fgetc(fp))!=EOF)
{
fwrite(testing, 1, sizeof(testing), fp);
fseek(fp, 1, SEEK_CUR); //increment pointer 1 byte
}
fclose(fp);
}
当我运行它时,会弹出一条错误消息说“Debug Assertion Failed!...Expression:("Invalid file open mode",0) 并打印“The program '[3896] filesystem.exe: Native' has exited with code 3 (0x3)。”