Solved

fgets

Posted on 1999-01-10
10
862 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 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

12 Experts available now in Live!

Get 1:1 Help Now