Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Problem with fgets reading last line over and over (http://www.velocityreviews.com/forums/t450308-problem-with-fgets-reading-last-line-over-and-over.html)

Trond Valen 12-06-2005 11:31 AM

Problem with fgets reading last line over and over
 
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

Karl Heinz Buchegger 12-06-2005 12:42 PM

Re: Problem with fgets reading last line over and over
 
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
kbuchegg@gascad.at

Niklas Norrthon 12-06-2005 12:42 PM

Re: Problem with fgets reading last line over and over
 
Trond Valen <valen@stud.ntnu.no> 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

mlimber 12-06-2005 01:49 PM

Re: Problem with fgets reading last line over and over
 
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


deane_gavin@hotmail.com 12-06-2005 02:10 PM

Re: Problem with fgets reading last line over and over
 

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


Niklas Norrthon 12-07-2005 08:23 AM

Re: Problem with fgets reading last line over and over
 
deane_gavin@hotmail.com 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


All times are GMT. The time now is 04:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.