fwrite 不断失败,不知道为什么
在我下面的代码中,据我所知,文件写入正确。当我查看文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
r+b
而不是r+
fseek(binaryFile, 0, SEEK_SET)
来“倒回”文件。rewind
也可以用于这种情况 -fseek
允许您将读/写指针放置在您想要的任何位置。r+b
instead ofr+
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.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 tobinaryFile
, 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.您在阅读文件之前忘记倒回文件:
You forgot to rewind your file before reading it:
当您完成对文件的写入时,FILE 指针位于文件的末尾,因此如果您尝试读取它当然是行不通的。在读取之前尝试使用
fseek
将指针移动到文件的开头。请避免这种情况:
并且更喜欢这种情况:
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 :
and prefer this: