【发布时间】:2022-06-16 03:37:56
【问题描述】:
我注意到 fread() 和 fwrite() 在我的程序中不起作用。我写了这个小程序来演示它。
#include <stdio.h>
typedef struct Product {
float size;
float price;
} Product;
int main() {
Product my_prod;
my_prod.price = 13.2;
my_prod.size = 10.3;
FILE* file_in = fopen("/Users/piton/Desktop/UniverProg/Test/Test/input.txt", "w");
if (file_in == NULL)
printf("ERROR");
fwrite(&my_prod, sizeof(Product), 1, file_in);
fclose(file_in);
return 0;
}
所以,我在 input.txt 中有输出:ÕÃ$A33SA
(是的,我将文件命名为“输入”,但实际上它是用于输出)
请帮忙
谢谢
【问题讨论】:
-
你应该使用
"wb"而不是"w"打开模式来处理二进制文件。 -
除了文件中还有什么?
-
嗯,根据IEEE-754 Floating Point Converter,正确的输出应该是
ÍÌ$A33SA(十六进制的cd cc 24 41 33 33 53 41)。 -
如果您使用
fread阅读此内容并打印出来会怎样?如果您检查内存位置,该结构的内容是什么? -
你为什么认为这是错误的?