Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

fgets

Posted on 1999-01-10
10
Medium Priority
?
874 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 

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

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!

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

609 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