The following program is working fine some times and some times getting garbages while reading contents from the files.
i tried it months ago
then i got the garbage results but now its working fine.
but i unable to understand whats wrong with it.
i have a proof when i dint get the out put properly.
nothing important but i would like to know the problem.
do we need to follow any specific procedure when writting and reading from binary files.
please some one rectify .
#include<stdio.h>
void add_record();
void display();
void display_all();
void edit_record();
int find(int);
void edit_name();
struct stud{
int rolno;
float fee;
char name[20];
};
int main() {
struct stud s;
int ch,c;
do {
__fpurge(stdin);
printf("1. Add Record\n");
printf("2. Display Record\n");
printf("3. Display_all\n");
printf("4. Edit record\n");
printf("Enter Choice\n");
scanf("%d",&ch);
switch(ch) {
case 1: add_record();
break;
case 2:display();
break;
case 3:display_all();
break;
case 4:edit_record();
break;
default :printf("Invalid choice\n");
break;
case -1:exit(0);
}
printf("Enter Y or y to continue\n");
__fpurge(stdin);
c=getchar();
}
while(c=='y'||c=='Y');
return 0;
}
void display_all()
{
struct stud s;
FILE *fp;
int count=0;
fp=fopen("Stu.dat","rb");
while(fread(&s,sizeof(s),1,fp)) {
printf("Name: %s\n",s.name);
printf("Rno: %d\n",s.rolno);
printf("Fee: %f\n",s.fee);
count++;
}
if(count<=0)
printf("File is Empty\n");
fclose(fp);
}
void display()
{
struct stud s;
FILE *fp;
int rno;
//unsigned char *fref=&rno;
fp=fopen("Stu.dat","rb");
printf("enter rno\n");
scanf("%d",&rno);
while(fread(&s,sizeof(s),1,fp)) {
if(s.rolno==rno) {
printf("Name: %s\n",s.name);
printf("Rno: %d\n",s.rolno);
printf("Fee: %f\n",s.fee);
fclose(fp);
return ;
}
}
printf("Record Not Found\n");
fclose(fp);
}
void add_record()
{
FILE *fp;
struct stud s;
fp=fopen("Stu.dat","ab");
if(fp==NULL)
{
printf("File cannot be opened\n");
}
__fpurge(stdin);
printf("\nEnter name:");
__fpurge(stdin);
fgets(s.name,sizeof(s.name),stdin);
s.name[strlen(s.name)-1]='\0';
printf("\nEnter rollno:");
scanf("%d",&s.rolno);
printf("\nEnter fee:");
scanf("%f",&s.fee);
if((fwrite(&s,sizeof(s),1,fp)!=1))
printf("Written Failed\n");
fclose(fp);
}
void edit_record()
{
char ch;
printf(" what do you want to edit\n");
do {
printf("Enter 1 to edit Name\n");
printf("Enter 2 to edit Rno\n");
printf("Enter 3 to edit Fee\n");
printf("Enter your chocie\n");
__fpurge(stdin);
scanf("%c",&ch);
switch(ch) {
case '1':
edit_name();
break;
#if 0
case 2:edit_rno();
break;
case 3:edit_fee();
break;
#endif
default:printf("Invalid choice\n");
break;
}
printf("\nEnter Y or y to continue\n");
__fpurge(stdin);
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
}
int find(int rno)
{
FILE *fp;
struct stud s;
if((fp=fopen("Stu.dat","rb"))!=NULL)
{
while(fread(&s,sizeof(s),1,fp))
{
if(s.rolno==rno)
fclose(fp);
return rno;
}
fclose(fp);
return -1;
}
printf("File_cannot_be_opened\n");
}
void edit_name()
{
int rno;
int change=0;
char name[20];
FILE *fp;
struct stud s;
printf("\nEnter Rno to Edit name:");
scanf("%d",&rno);
printf("Enter Name to change\n");
__fpurge(stdin);
fgets(name,sizeof(name),stdin);
if( ( find(rno) ) == rno )
{
if((fp=fopen("Stu.dat","rb+"))!=NULL)
{
while(fread(&s,sizeof(s),1,fp)) {
if(s.rolno==rno)
{
strcpy(s.name,name);
if((fwrite(&s,sizeof(s),1,fp))==1)
{
printf(" Name_Change_Success\n");
change=1;
}
break;
}
}
if(change!=1)
printf("Name not changed\n");
}
else
printf("Fopen_Fail\n");
}
else
printf("No such rollno is present\n");
fclose(fp);
}