hello guys, Im trying to understand how fread and fwrite works. My teacher said that these functions can store/output binaries and texts in files. I know how to do it with string. Im confused with floats and integers.
I tried to make a program for it but Im getting errors. I can't run my program. Can anyone tell me whats wrong? Here is my code.
int save(float d)
{
FILE *fp;
char fn[50];
printf("What is the filename? ");
flushall();
gets(fn);
if((fp = fopen(fn, "wb")) == NULL)
{
printf("Cannot save file");
return 0;
}
fwrite(d, sizeof(float), 1, fp);
printf("\nFile saved");
fclose(fp);
return 0;
}
int load()
{
FILE *fp;
float b;
char fn[50];
printf("What is the filename? ");
flushall();
gets(fn);
fp = fopen(fn, "rb");
fread(b, sizeof(float), 1, fp);
fclose(fp);
printf("%.2f", b);
return 0;
}