Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [URGENT] fgets reading last line in file twice

Reply
Thread Tools

[URGENT] fgets reading last line in file twice

 
 
DJP
Guest
Posts: n/a
 
      10-18-2004
Hi,

I need to read a file programmatically until end of file. My logic is as
follows:

while(!feof(Fp))
{

fgets(readLine,10000,Fp);

.
.
.
do something
.
.
.
}


However with this logic, depending on the file I am reading my program
sometime reads the last line twice. Can anyone please tell me why this is
happenning?

This is kind of urgent so your speedy help will be greatly appreciated.
Thank you!!


 
Reply With Quote
 
 
 
 
Vyacheslav Kononenko
Guest
Posts: n/a
 
      10-18-2004
DJP wrote:

> Hi,
>
> I need to read a file programmatically until end of file. My logic is as
> follows:
>
> while(!feof(Fp))
> {
>
> fgets(readLine,10000,Fp);
>
> .
> .
> .
> do something
> .
> .
> .
> }
>
>
> However with this logic, depending on the file I am reading my program
> sometime reads the last line twice. Can anyone please tell me why this is
> happenning?
>
> This is kind of urgent so your speedy help will be greatly appreciated.
> Thank you!!
>
>

feof() returns true AFTER you try to read past the end of file.
So this code should work better:
while(true) {
fgets(readLine,10000,Fp);
if( feof(Fp) ) break;
...
}

--
Regards,
Slava

 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      10-18-2004
DJP wrote:
> Hi,
>
> I need to read a file programmatically until end of file. My logic is as
> follows:
>
> while(!feof(Fp))

Here you are testing for EOF before the flag is set.
The flag is set by a read action. See the FAQ below.

> {
>
> fgets(readLine,10000,Fp);
>
> .
> .
> .
> do something
> .
> .
> .
> }
>
>
> However with this logic, depending on the file I am reading my program
> sometime reads the last line twice. Can anyone please tell me why this is
> happenning?
>
> This is kind of urgent so your speedy help will be greatly appreciated.
> Thank you!!
>
>


You'll get faster results by consulting the FAQ first.
I removed all but the C and C++ language newsgroups.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-18-2004

"DJP" <(E-Mail Removed)> wrote in message
news:cl0sc1$g7v$(E-Mail Removed)...
> Hi,
>
> I need to read a file programmatically until end of file. My logic is as
> follows:
>
> while(!feof(Fp))
> {
>
> fgets(readLine,10000,Fp);
>
> .
> .
> .
> do something
> .
> .
> .
> }


Your logic is very common but wrong

while (fgets(readLine,10000,Fp), !feof(fp))
{
do something
}

>
> However with this logic, depending on the file I am reading my program
> sometime reads the last line twice. Can anyone please tell me why this is
> happenning?


Because the return of feof only accurately reflects the status of the
previous read, not the next one.

It sometimes seems that every single newbie in the world gets this wrong, so
you are not alone.

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-18-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "DJP" <(E-Mail Removed)> wrote in message
> news:cl0sc1$g7v$(E-Mail Removed)...
>> Hi,
>>
>> I need to read a file programmatically until end of file. My logic is as
>> follows:
>>
>> while(!feof(Fp))
>> {
>>
>> fgets(readLine,10000,Fp);
>>
>> .
>> .
>> .
>> do something
>> .
>> .
>> .
>> }

>
> Your logic is very common but wrong
>
> while (fgets(readLine,10000,Fp), !feof(fp))
> {
> do something
> }
>


Actually I don't think that code is correct. My unfamiliarity with C.

Try this

while (fgets(readLine,10000,Fp))
{
}

john


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-18-2004

"DJP" <(E-Mail Removed)> wrote in message
news:cl0sc1$g7v$(E-Mail Removed)...
> Hi,
>
> I need to read a file programmatically until end of file. My logic is as
> follows:
>
> while(!feof(Fp))
> {
>
> fgets(readLine,10000,Fp);
> }
>
>
> However with this logic, depending on the file I am reading my program
> sometime reads the last line twice. Can anyone please tell me why this is
> happenning?


Hmm, we see this question very frequently here. That's why it's cited
in comp.lang.c's Frequently Asked Questions document:

http://www.eskimo.com/~scs/C-faq/q12.2.html

Once you read that and fix your code, start here:
http://www.eskimo.com/~scs/C-faq/top.html

and read the whole thing. It will save you hours of frustration
and wasted time.

-Mike


 
Reply With Quote
 
Rookie
Guest
Posts: n/a
 
      10-18-2004

>> I need to read a file programmatically until end of file. My logic is as
>> follows:
>>
>> while(!feof(Fp))
>> {
>>
>> fgets(readLine,10000,Fp);
>> }
>>
>>


Is this how I should fix it?

while( fgets(readLine,10000,Fp)!=EOF)
{
//do something
}


 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      10-18-2004
Rookie wrote:
>>>I need to read a file programmatically until end of file. My logic is as
>>>follows:
>>>
>>>while(!feof(Fp))
>>> {
>>>
>>> fgets(readLine,10000,Fp);
>>>}
>>>
>>>

>
>
> Is this how I should fix it?
>
> while( fgets(readLine,10000,Fp)!=EOF)
> {
> //do something
> }
>
>

No. Please read the FAQ entry that Mike mentioned.

HTH,
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-18-2004
"Rookie" <(E-Mail Removed)> writes:
>>> I need to read a file programmatically until end of file. My logic is as
>>> follows:
>>>
>>> while(!feof(Fp))
>>> {
>>>
>>> fgets(readLine,10000,Fp);
>>> }
>>>
>>>

>
> Is this how I should fix it?
>
> while( fgets(readLine,10000,Fp)!=EOF)
> {
> //do something
> }


No.

You should have some documentation somewhere on the fgets() function,
either a book or some form of online documentation such as a man page.
This documentation should tell you the type of the result returned by
the fgets() function. Go look it up; I'll wait.

....

Ok, now you know that fgets() returns a char* value. EOF is an int,
not a char*. That alone should tell you that EOF is not the
appropriate value to compare the result of fgets() to.

What is the appropriate value? Go back to your documentation and find
out what fgets() returns when there's no more input.

The types and values returned by functions in the C standard library
are often not obvious. Any time you write code that looks at the
result of a standard function, you need to have a copy of the
documentation next to you so you know what to expect, at least until
you have it all memorized (which you probably never will; I certainly
don't).

(By the way, I'm not trying to be condescending; I had to look up the
definition of fgets() myself before I wrote this.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      10-18-2004
>Is this how I should fix it?

No.

>while( fgets(readLine,10000,Fp)!=EOF)

while (fgets(readLine, 10000, Fp) != NULL)
>{
> //do something
>}


Gordon L. Burditt
 
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
newbie question: fgets() and feof() read last line twice Andy C Programming 3 03-19-2009 05:16 PM
while(!FP.eof()) is reading the last data point of the file twice ramana C Programming 5 12-26-2007 05:52 PM
Problem with fgets reading last line over and over Trond Valen C++ 5 12-07-2005 08:23 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