fwrite 不断失败,不知道为什么

发布于 2024-11-01 10:58:51 字数 1418 浏览 3 评论 0原文

在我下面的代码中,据我所知,文件写入正确。当我查看文件 floats.dat 时,我看到这个二进制流 ÍÌL@33c@ÍÌÜ@ffFAßOeA^@^@bBf6zE33äCff<83>BÍÌςB

但是我的程序最终总是触发这个 if 语句:

if(fread(inputFloats, sizeof(float), LENGTH, binaryFile) < LENGTH)
{
   fprintf(stderr, "Problem reading some or all data from %s\n\n", binaryFileName);
   return EXIT_FAILURE;
}

有人看到我在这里做错了什么吗?完整代码如下。

#include <stdlib.h>
#include <stdio.h>
#define LENGTH 10

int main(void)
{
   FILE *binaryFile, *textFile;
   char *binaryFileName = "floats.dat", *textFileName = "floats.txt";
   float floats[LENGTH] = {3.2, 3.55, 6.9, 12.4, 14.332, 56.5, 4003.4, 456.4, 65.7, 83.4};
   float inputFloats[LENGTH];
   int i;

   if((binaryFile = fopen(binaryFileName, "r+")) == NULL)
   {
      fprintf(stderr, "Problem opening %s", binaryFileName);
   }

   if(fwrite(floats, sizeof(float), LENGTH, binaryFile) < LENGTH)
   {
      fprintf(stderr, "Problem writing some or all data to %s\n", binaryFileName);
      return EXIT_FAILURE;
   }

   printf("DATA WRITTEN SUCCESSFULLY\n");

   if(fread(inputFloats, sizeof(float), LENGTH, binaryFile) < LENGTH)
   {
      fprintf(stderr, "Problem reading some or all data from %s\n\n", binaryFileName);
      return EXIT_FAILURE;
   }

   for(i = 0; i < LENGTH; i++)
   {
      printf("float[%d] = %f\n", i, floats[i]);
   }

   return EXIT_SUCCESS;
}

In my code below, the file is being written correctly as far as I can tell. When I look in the file floats.dat I see this stream of binary ÍÌL@33c@ÍÌÜ@ffFAßOeA^@^@bBf6zE33äCff<83>BÍ̦B

However my program always ends up triggering this if statement:

if(fread(inputFloats, sizeof(float), LENGTH, binaryFile) < LENGTH)
{
   fprintf(stderr, "Problem reading some or all data from %s\n\n", binaryFileName);
   return EXIT_FAILURE;
}

Does anybody see something I've done wrong here? Full code below.

#include <stdlib.h>
#include <stdio.h>
#define LENGTH 10

int main(void)
{
   FILE *binaryFile, *textFile;
   char *binaryFileName = "floats.dat", *textFileName = "floats.txt";
   float floats[LENGTH] = {3.2, 3.55, 6.9, 12.4, 14.332, 56.5, 4003.4, 456.4, 65.7, 83.4};
   float inputFloats[LENGTH];
   int i;

   if((binaryFile = fopen(binaryFileName, "r+")) == NULL)
   {
      fprintf(stderr, "Problem opening %s", binaryFileName);
   }

   if(fwrite(floats, sizeof(float), LENGTH, binaryFile) < LENGTH)
   {
      fprintf(stderr, "Problem writing some or all data to %s\n", binaryFileName);
      return EXIT_FAILURE;
   }

   printf("DATA WRITTEN SUCCESSFULLY\n");

   if(fread(inputFloats, sizeof(float), LENGTH, binaryFile) < LENGTH)
   {
      fprintf(stderr, "Problem reading some or all data from %s\n\n", binaryFileName);
      return EXIT_FAILURE;
   }

   for(i = 0; i < LENGTH; i++)
   {
      printf("float[%d] = %f\n", i, floats[i]);
   }

   return EXIT_SUCCESS;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

擦肩而过的背影 2024-11-08 10:58:51
  • 您不使用文本数据,因此应在打开文件时指定二进制模式。使用 r+b 而不是 r+
  • 您需要在写入后使用 fseek(binaryFile, 0, SEEK_SET) 来“倒回”文件。 rewind 也可以用于这种情况 - fseek 允许您将读/写指针放置在您想要的任何位置。
  • You're not working with text data so you should specify a binary mode when opening the file. Use r+b instead of r+
  • You need to fseek(binaryFile, 0, SEEK_SET) to "rewind" the file after writing. rewind can also be used for this case - fseek allows you to position the read/write pointer wherever you want.
甜宝宝 2024-11-08 10:58:51

FILE 结构记录了它当前指向的文件中的位置。由于您刚刚写入了 binaryFile,因此文件指针位于您所写入内容的末尾

因此,您需要在阅读之前使用 fseek(binaryFile, 0, SEEK_SET); 倒带文件。

The FILE structure keeps a record of where in the file it is currently pointing. Since you've just written to binaryFile, the file pointer is at the end of what you've written.

You therefore need to rewind the file, using fseek(binaryFile, 0, SEEK_SET); before you read.

丢了幸福的猪 2024-11-08 10:58:51

您在阅读文件之前忘记倒回文件:

rewind(binaryFile);

You forgot to rewind your file before reading it:

rewind(binaryFile);

当您完成对文件的写入时,FILE 指针位于文件的末尾,因此如果您尝试读取它当然是行不通的。在读取之前尝试使用 fseek 将指针移动到文件的开头。

请避免这种情况:

if((binaryFile = fopen(binaryFileName, "r+")) == NULL) {

并且更喜欢这种情况:

binaryFile = fopen(binaryFileName, "rb+");
if(!binaryFile) {

When you finish writing to the file, the FILE pointer is at the end of it, so of course if you try reading it will not work. Try using fseek to move the pointer to the beginning of the file before reading.

Please avoid this :

if((binaryFile = fopen(binaryFileName, "r+")) == NULL) {

and prefer this:

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