I have to write a program for writing and reading a binary file. There should be a menu for the choice. The file should have indentification number, an article with name, parameters, etc and a price. So it has to be int|sizeof char|char|double.
I have some issues with the code. It compiles. I open the file and write in it. But it "dont want" to read itself. When I remove the double slash comments to make the check the program prints the errors. I need help.
This is the code:
#include <stdio.h>
void writing();
void reading();
int main ()
{
char choice;
/* do
{
printf("Enter 1 for writening in file and 2 for reading!\n");
scanf("%c", &choice);
// choice=getchar();
fflush(stdin);
}
while(choice!='1'&&choice!='2');
switch(choice)
{
case '1':writing(); break;
case '2':reading(); break;
} */
for (;;)
{
printf("Enter a number\n");
printf("Choose 1 for writening and 2 for reading\n");
//choice=getchar();
scanf("%c", &choice);
fflush(stdin);
if (choice=='1'||choice=='2')
break;
}
switch(choice)
{
case '1': writing (); break;
case '2': reading (); break;
}
return 0;
}
void writing ()
{
int j,i; // size of article
int number;
char article[50];
double price;
FILE*fp;
printf("Enter a string less than 50\n");
gets(article);
//scanf("%c", &article);
j=(sizeof (article));
printf("Enter a number:\n");
scanf("%d", &number);
printf("Enter a price:\n");
scanf("%lf", &price);
if (fp=fopen ("writing.data","ab")==NULL)
{
printf("Couldnt open the file\n");
}
fwrite(&number,sizeof (int),1,fp);
/* if(fwrite(&number,sizeof (int),1,fp)!=1)
{
printf("Error\n");
} */
fwrite(&j,sizeof(int),1,fp);
/* if(fwrite(&j,sizeof (int),1,fp)!=1)
{
printf("Error\n");
} */
fwrite(article,sizeof (article),1,fp);
/* if(fwrite(article,sizeof (article),1,fp)!=1)
{
printf("Error\n");
} */
fwrite(&price,sizeof (double),1,fp);
/*if(fwrite(&price,sizeof (double),1,fp)!=1)
{
printf("Error\n");
} */
fclose(fp);
}
void reading()
{
int i; // size of article
int number;
int number1;
char article[50];
double price;
FILE*fp;
printf("Enter a number to start the reading:");
scanf("%d", &number1);
if (fp=fopen ("writing.data","rb")==NULL)
{
printf("Couldnt open the file\n");
}
for (;;)
{
fread(&number,sizeof(int),1,fp);
/* if(fread(&number,sizeof(int),1,fp)!=1)
{
printf("Error");
break;
} */
fread(&i,sizeof(int),1,fp);
/* if(fread(&i,sizeof(int),1,fp)!=1)
{
printf("Error");
exit(1);
} */
fread(article,sizeof(article),1,fp);
/* if(fread(article,sizeof(article),1,fp)!=1)
{
printf("Error");
exit(2);
} */
fread(&price,sizeof (double),1,fp);
/* if(fread(&price,sizeof (double),1,fp)!=1)
{
printf("Error");
exit(3);
} */
if(number==number1)
{
printf("%d", number);
printf(article);
printf("%lf", price);
}
}
fclose(fp);
}
Ok, so i edited the parenthesis and it works. But without the checks. Thats the reason i commended them. It seems i have problem with them, can u give me more info about the checks in C and help me get these ones work.
These are the warnings warnings
*Edit2
I typed the code from the zero again and it works. I fixed the checks.
Copyright Notice:Content Author:「Dimitar Bochev」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36032428/binary-file-menu-writing-and-reading-in-c-issue