• C

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.

prabhutAsked:
Who is Participating?
 
cpa802Connect With a Mentor Commented:
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
 
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
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

 
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
 
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
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.

All Courses

From novice to tech pro — start learning today.