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!