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: 879
  • Last Modified:

fgets

I have written a small program to read a file line by line and to display it(as soon as it is read). The program flow is like this, I will open a file, until EOF I will read the file into a buffer of size 100 and print the buffer. At the EOF, I will close the file and exit. I am reading a text file whose content is just first,second, third, fourth and fifth(each in a separate line). When I ran the program, I got six lines printed, why so???.

     Here is the sample output :

1. first
2. second
3. third
4. fourth
5. fifth
6.

0
prabhut
Asked:
prabhut
  • 4
  • 2
  • 2
  • +2
1 Solution
 
pagladasuCommented:
Could you post your code.
0
 
pagladasuCommented:
Perhaps while creating the file, you had pressed the enter key after the last line before saving it.
0
 
prabhutAuthor Commented:
Here is my sample program :

#include <stdio.h>

main()
{
  FILE *fp;
  int i=1;
  char buf[100];

  fp = fopen("./file.txt","r");

  while ( !feof(fp) ) {
     fgets(buf,sizeof(buf), fp);
     printf("%d:%s\n",i,buf);
     i++;
     memset(buf,'\0',sizeof(buf));
  }

  fclose(fp);
}


Content of file.txt :

first
second
third
fourth
fifth
0
Technology Partners: 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!

 
ozoCommented:
while( fgets(buf,sizeof(buf), fp), !feof(fp) ){
          printf("%d:%s\n",i,buf);
          i++;
          memset(buf,'\0',sizeof(buf));
 }
0
 
pagladasuCommented:
ozo, I am slow
0
 
prabhutAuthor Commented:
I have implemented and found it is working fine. I have doubt, why it is not working in my code. I need a detail answer, please.
0
 
ozoCommented:
the 5th fgets stops at the last character in the file,
the 6th fgets detects an end of file, and reads nothing, which you print,
then you test the end of file condition and stop.
but fgets(buf,sizeof(buf), fp), !feof(fp) isn't really the best way to do this,
since it will miss a last line that's not terminated with a new-line character.
I'd instead do
while( fgets(buf,sizeof(buf), fp) ){ ... }
0
 
pagladasuCommented:
well done ozo.
0
 
thresher_sharkCommented:
As usual :-)
0
 
cpa802Commented:
I think this is your problem: you have a newline at the end of the fith line.

feof wont be set untill you read "PAST" the end of the file and fgets stops when it finds eof OR a newline. Some editors force you to have a newline at the end of the file (like "edt" on VMS)

You code is good but is bad style, Ususally you should write it like this

(PSEUDOCODE)

read String
while(!EOF)
{
    write String
    read String
}
if(String ! Empty) write String

I hope this helps
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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