Sunday 19 November 2017

C file handling record search : last record displayed twice

I wrote simple code in c to store student info ( roll
number, name,course, fee, department) in a text file student.txt -- code
snippet:



FILE
*fp;
fp=fopen("student.txt","r");
//Input details from user and
..
//store it in student.txt
fprintf(fp,"%d %s %s %d %s
",s.rollno,s.name,s.course,s.fee,s.dept);



And
I wrote following code to retrieve and print all records from file, AND it retrieve last
record TWICE !



while
(!feof(fp))
{

fscanf(fp,"%d%s%s%d%s",&s.rollno,s.name,s.course,&s.fee,s.dept);


printf("%d %s %s %d
%s\n",s.rollno,s.name,s.course,s.fee,s.dept);



}

//OUTPUT :
46 mustafa be 12000 cse
41 Sam
BE 32000 CSE
42 Howard BE 25000 EE
44 Sheldon BE 25000 CSE

44 Sheldon BE 25000
CSE



Why
last record(Sheldon..) is read twice from file (though its written only once in file, I
checked). Please help, really stuck.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...