Solved

fgets

Posted on 1999-01-10
10
863 Views
Last Modified: 2010-08-05
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
Comment
Question by:prabhut
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 1255738
Could you post your code.
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1255739
Perhaps while creating the file, you had pressed the enter key after the last line before saving it.
0
 

Author Comment

by:prabhut
ID: 1255740
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
 
LVL 84

Expert Comment

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

Expert Comment

by:pagladasu
ID: 1255742
ozo, I am slow
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:prabhut
ID: 1255743
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
 
LVL 84

Expert Comment

by:ozo
ID: 1255744
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 1255745
well done ozo.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1255746
As usual :-)
0
 

Accepted Solution

by:
cpa802 earned 20 total points
ID: 1255747
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 and use structures 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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now