I've got a problem when i try to write data to a binary file. This is the code:
当我尝试将数据写入二进制文件时,我遇到了问题。这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct
{
char name[255];
int quantity;
float price;
} product;
int main()
{
product x;
FILE *f;
strcpy(x.name,"test");
x.quantity=10;
x.price=20.0;
f=fopen("test.txt","wb");
fwrite(&x,sizeof(x),1,f);
fclose(f);
return 0;
}
When I run the program,it only writes the x.name string,ignoring the other 2(quantity and price). I've googled it and this seems to be the correct function to write data to a binary file...but it still doesn't work for me. What should I do? Thanks in advance!
当我运行程序时,它只写x.name字符串,忽略其他2(数量和价格)。我用google搜索它,这似乎是将数据写入二进制文件的正确函数......但它仍然不适合我。我该怎么办?提前致谢!
2 个解决方案
#1
2
Your function works fine, the problem is that you write a lot of unused data, are not using the right tool to view your binary file.
您的功能正常,问题是您编写了大量未使用的数据,没有使用正确的工具来查看您的二进制文件。
You put "test"
into the name, which has a size of 255 characters. This uses up the first five (four letters plus null terminator) while the remaining 250 characters are unused. They are written to the file, and their content become "junk filling" between "test"
and the other data.
您将“test”放入名称中,该名称的大小为255个字符。这会占用前五个(四个字母加上空终止符),而剩余的250个字符未使用。它们被写入文件,它们的内容在“test”和其他数据之间变成“垃圾填充”。
If you write a simple program to read your file back, you would discover that both the quantity and the price are set correctly to the values that you wrote:
如果你编写一个简单的程序来读回你的文件,你会发现数量和价格都正确地设置为你写的值:
int main()
{
product x;
FILE *f;
f=fopen("test.txt","rb");
fread(&x,sizeof(x),1,f);
fclose(f);
printf("'%s' - %d %f\n", x.name, x.quantity, x.price);
return 0;
}
#2
0
According to your code you are trying to write address of x. But if you want to write full object then you have to serialize the object first.
根据你的代码,你试图写x的地址。但是如果要编写完整对象,则必须先对序列化对象。
#1
2
Your function works fine, the problem is that you write a lot of unused data, are not using the right tool to view your binary file.
您的功能正常,问题是您编写了大量未使用的数据,没有使用正确的工具来查看您的二进制文件。
You put "test"
into the name, which has a size of 255 characters. This uses up the first five (four letters plus null terminator) while the remaining 250 characters are unused. They are written to the file, and their content become "junk filling" between "test"
and the other data.
您将“test”放入名称中,该名称的大小为255个字符。这会占用前五个(四个字母加上空终止符),而剩余的250个字符未使用。它们被写入文件,它们的内容在“test”和其他数据之间变成“垃圾填充”。
If you write a simple program to read your file back, you would discover that both the quantity and the price are set correctly to the values that you wrote:
如果你编写一个简单的程序来读回你的文件,你会发现数量和价格都正确地设置为你写的值:
int main()
{
product x;
FILE *f;
f=fopen("test.txt","rb");
fread(&x,sizeof(x),1,f);
fclose(f);
printf("'%s' - %d %f\n", x.name, x.quantity, x.price);
return 0;
}
#2
0
According to your code you are trying to write address of x. But if you want to write full object then you have to serialize the object first.
根据你的代码,你试图写x的地址。但是如果要编写完整对象,则必须先对序列化对象。