LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 04: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, 05: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, 06:43 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 06: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



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 10:19 PM
ferror and feof segmentation fault nc3b Programming 3 07-22-2007 11:32 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
function who remplace feof linuxanswer Programming 1 04-22-2004 06:19 PM
C fscanf / feof question. Claus Programming 1 02-02-2004 11:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:09 PM.

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
Open Source Consulting | Domain Registration