Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with fgets reading last line over and over

Reply
Thread Tools

Problem with fgets reading last line over and over

 
 
Trond Valen
Guest
Posts: n/a
 
      12-06-2005
Hi!

Anyone who knows what the reason might be? fgets is supposed to return
null when reading EOF. I have a test for this, so my while loop should
exit, but it doesn't ...

Trond
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      12-06-2005
Trond Valen wrote:
>
> Hi!
>
> Anyone who knows what the reason might be? fgets is supposed to return
> null when reading EOF. I have a test for this, so my while loop should
> exit, but it doesn't ...
>


Rule #1: Post Code

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-06-2005
Trond Valen <(E-Mail Removed)> writes:

> Hi!
>
> Anyone who knows what the reason might be? fgets is supposed to return
> null when reading EOF. I have a test for this, so my while loop should
> exit, but it doesn't ...


You forgot to post any code illustrating the observed behavior, so it
is very difficult to know what the reason might be.

The following should work:

#include <stdio.h>

int main()
{
char buf[42];
char* ptr;

while ((ptr = fgets(buf, sizeof buf, stdin)) != NULL) {
/* do something with buf here */
}
return 0;
}

/Niklas Norrthon
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      12-06-2005
Trond Valen wrote:
> Hi!
>
> Anyone who knows what the reason might be? fgets is supposed to return
> null when reading EOF. I have a test for this, so my while loop should
> exit, but it doesn't ...
>
> Trond


Rule #2: Prefer iostreams to C-style file streams:

http://www.parashift.com/c++-faq-lit....html#faq-15.1

Cheers! --M

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-06-2005

Trond Valen wrote:
> Hi!
>
> Anyone who knows what the reason might be? fgets is supposed to return
> null when reading EOF. I have a test for this, so my while loop should
> exit, but it doesn't ...
>
> Trond


Rule #3: Make sure you test eof correctly with your C++ streams

http://www.parashift.com/c++-faq-lit....html#faq-15.5

Gavin Deane

 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-07-2005
(E-Mail Removed) writes:

> Trond Valen wrote:
> > Hi!
> >
> > Anyone who knows what the reason might be? fgets is supposed to return
> > null when reading EOF. I have a test for this, so my while loop should
> > exit, but it doesn't ...
> >
> > Trond

>
> Rule #3: Make sure you test eof correctly with your C++ streams
>

Rule #4: If you for some reason use C style I/O, make sure you test
eof correctly with your FILE objects.

/Niklas Norrthon
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I assume fgets won't modify last bytes of output array if unused ? Francis Moreau C Programming 17 01-04-2011 01:33 AM
newbie question: fgets() and feof() read last line twice Andy C Programming 3 03-19-2009 05:16 PM
[URGENT] fgets reading last line in file twice DJP C Programming 16 10-21-2004 11:19 AM
[URGENT] fgets reading last line in file twice DJP C++ 7 10-21-2004 09:23 AM
fscanf or fgets still misses last line unless there is a newline Charles Erwin C Programming 4 10-06-2003 08:12 PM



Advertisments