C - fwrite() 不输出到文件

发布于 2024-10-06 08:42:04 字数 704 浏览 0 评论 0原文

从未使用过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忱杏 2024-10-13 08:42:04

您已打开文件进行读取(这就是 fopen("Drive2MB", "r");r 的含义)。您不能写入为读取而打开的文件。

You have opened the file for reading (that's what the r stands for in fopen("Drive2MB", "r");). You may not write to a file opened for reading.

清风疏影 2024-10-13 08:42:04

您正在以只读模式打开它

使用 r+ 作为 fopen

You're opening it in read only mode

Use r+ for the fopen

雨后咖啡店 2024-10-13 08:42:04
fp = fopen("Drive2MB", "r")

您以只读方式打开文件

尝试

fp = fopen("Drive2MB", "r+"); 
fp = fopen("Drive2MB", "r")

your openning your file in read only

try

fp = fopen("Drive2MB", "r+"); 
雪花飘飘的天空 2024-10-13 08:42:04

您已使用 fopen 的“r”部分打开文件进行读取。要写入文件,您可以以读/写模式或写入模式打开它。

// Read/write mode
fp = fopen("Drive2MB", "r+");

// Write only mode
fp = fopen("Drive2MB", "w");

我个人从来不喜欢使用“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.

// Read/write mode
fp = fopen("Drive2MB", "r+");

// Write only mode
fp = fopen("Drive2MB", "w");

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.

偷得浮生 2024-10-13 08:42:04
  1. 使用 fopen r+ 或 w+ 打开文件。
  2. fwrite完成后使用fflush将数据刷新到磁盘。
  3. fwrite完成后使用ferr检查文件流是否有问题。
  4. 检查磁盘是否有足够的可用空间。

我用3、4解决了这个问题。

  1. Use fopen r+ or w+ to open the file.
  2. Use fflush to flush data to disk after fwrite is complete.
  3. Use ferror to check if there is any problem with the file stream after fwrite is complete.
  4. Check whether the disk has enough free space.

I solved the problem with 3, 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文