473,243 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,243 software developers and data experts.

[URGENT] fgets reading last line in file twice

DJP
Hi,

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

while(!feof(Fp))
{

fgets(readLine,10000,Fp);

Jul 22 '05
7 10054
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

Jul 22 '05
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

Jul 22 '05

"DJP" <do***********@rediffmail.com> wrote in message
news:cl**********@gist.usc.edu...
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
Jul 22 '05

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de...

"DJP" <do***********@rediffmail.com> wrote in message
news:cl**********@gist.usc.edu...
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
Jul 22 '05
DJP wrote:
Hi,

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


use
while(fgets(readLine,10000,Fp))
{
/* do something */
}

If this seems off-topic in your newsgroup I apologize to comp.lang.c++,
comp.protocols.tcp-ip, comp.unix.programmer, and comp.unix.solaris, all
of which DJP cross-posted. I have no idea what newsgroups he actually
reads, and so respond to all his listed newsgroups. It is, however,
clear that he has not bothered to check the FAQs on past traffic in any
of these before posting. I hope he corrects his shotgun posting and
corrects his failure to behave like a human being by checking the FAQs
and past traffic before posting.
Jul 22 '05
"DJP" <do***********@rediffmail.com> writes:
Hi,

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

while(!feof(Fp))


FAQ. From the comp.lang.c FAQ list,
http://www.eskimo.com/~scs/C-faq/top.html :

12.2: Why does the code

while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}

copy the last line twice?

A: In C, end-of-file is only indicated *after* an input routine has
tried to read, and failed. (In other words, C's I/O is not like
Pascal's.) Usually, you should just check the return value of
the input routine -- fgets(), for example, returns NULL on end-
of-file. In virtually all cases, there's no need to use feof()
at all.

References: K&R2 Sec. 7.6 p. 164; ISO Sec. 7.9.3, Sec. 7.9.7.1,
Sec. 7.9.10.2; H&S Sec. 15.14 p. 382.

--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: yb****@cq.vasa.vg
Jul 22 '05
John Harrison wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de...
"DJP" <do***********@rediffmail.com> wrote in message
news:cl**********@gist.usc.edu...
I need to read a file programmatically until end of file.
My logic is as follows: while(!feof(Fp))
{
fgets(readLine,10000,Fp);
(snip)Your logic is very common but wrong while (fgets(readLine,10000,Fp), !feof(fp))
{
Actually I don't think that code is correct. My unfamiliarity with C. Try this while (fgets(readLine,10000,Fp))
{
}


As someone else noted, sorry for posting to so many groups.

The reason the latter is preferred is that it also takes
care of the I/O error case, where fgets() returns null,
but EOF has not been reached. An uncorrectable error would
otherwise result in an infinite loop. After the loop one might
test both feof() and ferror(), but most of the time I would
prefer to exit in both cases.

-- glen

Jul 22 '05

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Charles Erwin | last post by:
Is there any way, upon scanning in a file line by line to avoid missing the last line if there is not a newline character (aka you have to hit return on the last line of input in your file). I was...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
16
by: DJP | last post by:
Hi, I need to read a file programmatically until end of file. My logic is as follows: while(!feof(Fp)) { fgets(readLine,10000,Fp);
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
9
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
14
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str'...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.