LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-02-2008, 05:38 AM   #1
sureshsg
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
Question regarding feof() function.


following is my code.

#include<stdio. h>
main() {
FILE *s,*d;
char a[60];
s=fopen("temp" ,"r");
while(!feof( s)) {
fgets(a,55,s) ;
printf(" %s is the string in the temp file\n",a);
}
}

output:

$ ./a.out
hsuresh is a good boy.
is the string in the temp file
hsuresh is a good boy.
is the string in the temp file

tempfilecontents:
[vskumar@dotools files]$ cat temp
hsuresh is a good boy.

------------ --------- --------- ----

Why the contents of the file temp is getting printed twice?
 
Old 09-02-2008, 06:39 PM   #2
camh
Member
 
Registered: Feb 2005
Distribution: Slack/Debian
Posts: 163
Blog Entries: 2

Rep: Reputation: 33
Pull the printf out of your while loop and it should work:
Code:
#include <stdio.h>
main() {
 FILE *s;
 char a[60];
 s=fopen("temp","r");
 while(!feof(s)) {
  fgets(a,55,s);
 }
 printf("%s is the string in the temp file\n",a);
}
 
Old 09-02-2008, 07:43 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
See here: http://en.wikipedia.org/wiki/Fgets
Looks like you've got a blank line after your text line. fgets stops reading at new-line character, so it'll read the blank line, but put nothing into 'a', so you'll get the same content again.
 
Old 09-02-2008, 07:59 PM   #4
MarkByers
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Rep: Reputation: 0
fgets returns null if a line cannot be read. You should check for this.

Code:
#include<stdio.h>
main() {
  FILE *s,*d;
  char a[60];
  s=fopen("temp" ,"r");
  while(fgets(a,55,s)) {
    printf(" %s is the string in the temp file\n",a);
  }
}

feof can only see that the end of file has been reached after you attempt to read beyond the end of the file.

From feof documentation:

Checks whether the End-of-File indicator associated with stream is set, returning a value different from zero if it is.
This indicator is generally set by a previous operation on the stream that reached the End-of-File.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 11:19 PM
ferror and feof segmentation fault nc3b Programming 3 07-22-2007 12:32 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 02:31 PM
function who remplace feof linuxanswer Programming 1 04-22-2004 07:19 PM
C fscanf / feof question. Claus Programming 1 02-03-2004 12:55 AM


All times are GMT -5. The time now is 07:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration