Something wrong with output
Hi all. I'm a beginner in C so please bear with my noobness.
My code outputs characters like these when I run my program: ◄úÄ┬» even when it's supposed to output readable strings...
To illustrate here are sample inputs and outputs:
Example inputs
ENTER STUDENT ID :123456789
ENTER NAME: huh whatever
ENTER SCORE: 0
ENTER COURSE : phd
ENTER STUDENT ID :987654321
ENTER NAME: meh whatever
ENTER SCORE: 1
ENTER COURSE : phd
The output's fine if only the first record is entered / modified. But once I add the 2nd record and try to modify it, the output suddenly becomes wrong.
An example modification
Change this:
Code:
STUDENT ID NAME SCORE COURSE
987654321 meh whatever 1 phd
To this:
Code:
STUDENT ID NAME SCORE COURSE
111111111 lalala whatever 100 phd
Example output after modification
Code:
STUDENT ID NAME SCORE COURSE
123456789 huh whatever 0 phd
3543143 ±╟ìú┬Yever 31 54
I wonder why the output's like this. I think the problem has something to do with the fseek function I used? Please help me.
Here's my code for adding one record:
Code:
void add_record()
{
record stud;
char ch;
FILE *fp;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
clrscr();
gotoxy(35,1);printf("\n\nADD RECORD\n\n");
do
{
printf("\nStudent ID = ");
scanf("%lld", &stud.sid);
printf("\nName = ");
scanf("%s", &stud.name);
printf("\nScore = ");
scanf("%s", &stud.score);
printf("\nCourse = ");
scanf("%s", &stud.course);
fseek(fp,0,SEEK_END);
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nAdd another record? Y if yes\n\n");
ch=toupper(getche());
} while(ch=='Y');
fclose(fp);
getch();
}
Displaying all records:
Code:
void display_all()
{
record stud;
FILE *fp;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
clrscr();
printf("STUDENT ID\t%-16s%-12s%-12s\n\n","NAME","SCORE", "COURSE ");
while(fread( &stud,sizeof(stud),1,fp))
{
printf("%lld\t", stud.sid);
printf("%-16s", stud.name);
printf("%-12s", stud.score);
printf("%-12s\n", stud.course);
}
fseek(fp,0,SEEK_END);
printf("\n\nPress any key to go back to menu.");
getch();
}
Modifying one record:
Code:
void modify_record()
{
record stud;
long long int sid;
int i=0;
int j=0;
int x=0;
FILE *fp;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
clrscr();
gotoxy(35,2);printf("\n\nMODIFY STUDENT RECORD.");
printf("Enter student ID = ");
scanf("%lld", &sid);
while(fread(&stud,sizeof(stud),1,fp))
{
i++;
if(sid==stud.sid)
{
x++;
j=i-1;
printf("\n\n\n\nORIGINAL RECORD:\n");
printf("\nStudent ID = %lld", stud.sid);
printf("\nName = %s,", stud.name);
printf("\nScore = %d", stud.score);
printf("\nCourse = %s", stud.course);
fseek(fp,sizeof(stud)*j,SEEK_SET);
printf("\n\n\n\nENTER NEW RECORD...\n");
printf("\nStudent ID = ");
scanf("%lld", &stud.sid);
printf("\nName = ");
scanf("%s", &stud.name);
printf("\nScore = ");
scanf("%s", &stud.score);
printf("\nCourse = ");
scanf("%s", &stud.course);
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nRecord successfully modified!");
getch();
}
fseek(fp,sizeof(stud)*i,SEEK_SET);
}
if(x==0)
{
printf("\n\nNo record found...");
getch();
}
fclose(fp);
}
Thanks for any help...