C - fwrite() 不输出到文件
从未使用过 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);
}
当我运行此代码时,会弹出一条错误消息,显示“调试断言失败!...表达式:(“无效的文件打开模式”,0)并打印“程序'[3896] filesystem.exe:本机'已退出,代码为 3 (0x3)。”
Never used fwrite(), so I'm not sure exactly what I'm doing wrong. I'm just testing it now and all I want to do is try to write a single char out to a file until it reaches the end. The file I'm writing to is one I downloaded from my teacher's website. When I check the properties of it, the type is only listed as "file". It's supposed to just be an empty 2MB file for us to write our code to (file system lab if anyone's wondering). Here's my code:
#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);
}
When I run this, an error message pops up saying "Debug Assertion Failed!...Expression:("Invalid file open mode",0) and prints "The program '[3896] filesystem.exe: Native' has exited with code 3 (0x3)."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已打开文件进行读取(这就是
fopen("Drive2MB", "r");
中r
的含义)。您不能写入为读取而打开的文件。You have opened the file for reading (that's what the
r
stands for infopen("Drive2MB", "r");
). You may not write to a file opened for reading.您正在以只读模式打开它
使用 r+ 作为 fopen
You're opening it in read only mode
Use r+ for the fopen
您以只读方式打开文件
尝试
your openning your file in read only
try
您已使用 fopen 的“r”部分打开文件进行读取。要写入文件,您可以以读/写模式或写入模式打开它。
我个人从来不喜欢使用“rw”。当您打开一个文件时,它确实应该有一个被打开的原因。您也不需要调用 fseek 来移动到文件的开头,也不需要使用 fseek 来前进文件指针。 fopen 会自动将其打开到文件的开头,而 fwrite 会自动前进指针。仅当您在文件内部“查找”以到达特定点时才应使用 fseek。
在您给出的情况下,您只需要写入(“w”)模式,因为您从未从文件中读取。
You've opened the file for reading with the "r" part of fopen. To write to the file, you can open it in read/write mode or write mode.
I never like to use "rw" personally. When you open a file, it really should have one reason to be opened. You also do not need to call fseek to move to the start of the file and you do not need to use fseek to advance the file pointer. fopen will automatically open it to the start of the file and fwrite will automatically advance the pointer. fseek should only be used if you are "seek"ing inside of the file to get to a specific point.
In the case you've given, you would only need write ("w") mode since you are not ever reading from the file.
我用3、4解决了这个问题。
I solved the problem with 3, 4.