• C

Help me with reading file by C

could anyone explain for me this problem? I use C to read a text file and print its content out on screen, it sounds easy, ha :). however, I get a problem that it always double the last line in the file.
for ex, my file contains:
123 ten ho noisinh
156 Quang To QuiNhon

the result I get on screen is:
123 ten ho noisinh
156 Quang To QuiNhon
156 Quang To QuiNhon
or
123 ten ho noisinh
156 Quang To QuiNhon
156

and this is my code:
FILE *fptr;
char doc[200];
fptr = fopen("list.dat", "r+");

while(!feof(fptr))
{
  fgets(doc, 200, fptr);
  printf("%s\n", doc);
}
fclose(fptr);

Does it come from buffer ???
quangtovanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
FILE *fptr;
char doc[200];
fptr = fopen("list.dat", "r+");

while(!feof(fptr))
{
 fgets(doc, 200, fptr);
if(!feof(fp))   // <------- small change
 printf("%s", doc);  //<-- removed '\n'
else break;    // <-- else break out of loop
}
fclose(fptr);

actually EOF flag is set at the first unsuccessful read .. thats the reason why u have to check before printf

OR easier way .. since u r using fgets.. check in the man page its return value

while(fgets(doc, 200, fptr))
{
 printf("%s", doc);  //<-- removed '\n'
}
fclose(fptr);

0
HermeticCommented:
couldn't you just use

do
{
     fgets(doc,200,fptr);
     printf("%s",doc);
}
while (!feof(fptr));
0
akshayxxCommented:
as i already mentioned
when using fgets ..  following is enough .. no need to check for eof also ..

while(fgets(doc, 200, fptr))
{
printf("%s", doc);
}

for the following reason
gets() and fgets() return s on success, and NULL on error or  when  end
       of file occurs while no characters have been read.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ewestCommented:
Hermetic: that code will die a horrible death if the fgets()  fails. The standard idiomatic way to read a file is a suggested by akshayxx at the end of his response.
0
quangtovanAuthor Commented:
Thanz everyone, I tried code of akshayxx, it works properly
0
HermeticCommented:
You might think about awarding the points to akshayxx since he took the time to answer your question.
0
akshayxxCommented:
u r welcome guangtovan,
but i dont understand the reason behind 'B' grade, though my answer solved ur problem completely.
0
akshayxxCommented:
if u think that u did this by mistake then u can ask in community support to change the grade of this question
0
quangtovanAuthor Commented:
I'm really sorry, akshayxx. This is my first time joining this forum, so I made some mistakes. I gonna correct it. Apologize again, Quang.
0
akshayxxCommented:
no need to apologize .. it happens
one my reason of saying this was .. if someone searches EE database for similar problem and if he come across this  ( PAQ= previosly asked question) .. then  he might not even look at it for its being graded as B ..
though it was very simple problem ..

also it wont take extra points out of ur pockets ..
wish u luck , so good that u wont need EE for petty problems.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.