Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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 ???
0
quangtovan
Asked:
quangtovan
  • 5
  • 2
  • 2
  • +1
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now