?
Solved

Help me with reading file by C

Posted on 2003-03-16
10
Medium Priority
?
230 Views
Last Modified: 2010-04-15
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 ???
0
Comment
Question by:quangtovan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8148958
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
 
LVL 1

Expert Comment

by:Hermetic
ID: 8148972
couldn't you just use

do
{
     fgets(doc,200,fptr);
     printf("%s",doc);
}
while (!feof(fptr));
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 150 total points
ID: 8149042
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:ewest
ID: 8149043
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
 

Author Comment

by:quangtovan
ID: 8149181
Thanz everyone, I tried code of akshayxx, it works properly
0
 
LVL 1

Expert Comment

by:Hermetic
ID: 8149232
You might think about awarding the points to akshayxx since he took the time to answer your question.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8149328
u r welcome guangtovan,
but i dont understand the reason behind 'B' grade, though my answer solved ur problem completely.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8149340
if u think that u did this by mistake then u can ask in community support to change the grade of this question
0
 

Author Comment

by:quangtovan
ID: 8149372
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8149422
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question