help to detect errors in this simple proggramme
This code compiles fine.But
the data I tried to save to the file,' store.txt' are not permanantly stored. i could assert it by running this code block several times(more than 4), by the output produced by line a,b,c&d.
Can you plese help me to detect the reasons for why it doesn't get stored them permanantly.
Code:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
struct customer readCustomer();
struct slot readSlot();
struct slot{int day,period;};
struct customer{
char name[50],tel[10], paidOrNot[8];};
struct booking{
char freeOrNot;//if not free, gives 'n'....
struct slot slot_;
struct customer customer_;
};
struct bookingFile{
struct booking array[7][10];
};
main(){
FILE *fp;
struct bookingFile bf;
struct slot s;
fp=fopen("store.txt","r+");
if(fp==NULL){
printf("cannot open the file");
exit(0);
}
fread(&bf,sizeof(struct bookingFile),1,fp);
do{ //validating whether the given slot is free or not....
s=readSlot();
if(bf.array[s.day][s.period].freeOrNot=='n')
printf("Sorry,the slot you gave has been already booked.Try another slot!\n");
}while(bf.array[s.day][s.period].freeOrNot=='n');
bf.array[s.day][s.period].freeOrNot=0;
bf.array[s.day][s.period].slot_=s;
bf.array[s.day][s.period].customer_=readCustomer();
rewind(fp);
fwrite(&bf,sizeof(struct bookingFile),1,fp) ;
rewind(fp);
printf("%s\n",bf.array[0][0].customer_.tel);//line a
printf("%s\n",bf.array[1][1].customer_.tel);//line b
printf("%s\n",bf.array[2][2].customer_.tel);//line c
printf("%s\n",bf.array[3][3].customer_.tel);//line d
fclose(fp);
}
struct customer readCustomer(){
struct customer c;
printf("Enter your name: ");
gets(c.name);
printf("Enter the telephone number: ");
gets(c.tel);
printf("Are you paying ,just now?(Yes/No):");
gets(c.paidOrNot);
return c;
}
struct slot readSlot(){
struct slot s;
printf("Enter the number of day you want\n1.mon\n2.tues\n3.wed\n4.thu\n5.fri\n6.sat\n7.sun: ");
scanf("%i",&s.day);
printf("Enter the number of period you want\n1.8-9\n2.9-10\n3.10-11\n4.11-12\n5.12-1\n6.1-2\n7.2-3\n8.3-4\n9.4-5\n10.5-6: ");
scanf("%i",&s.period);
return s;
}
This code compiles fine.But
the data I tried to save to the file,' store.txt' are not permanantly stored. i could assert it by running this code block several times(more than 4), by the output produced by line a,b,c&d.
Can you plese help me to detect the reasons for why it doesn't get stored them permanantly.
Comment