473,782 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[URGENT] fgets reading last line in file twice

DJP
Hi,

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

while(!feof(Fp) )
{

fgets(readLine, 10000,Fp);

Jul 22 '05 #1
7 10277
DJP wrote:
Hi,

I need to read a file programmaticall y 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 #2
DJP wrote:
Hi,

I need to read a file programmaticall y 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.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3

"DJP" <do***********@ rediffmail.com> wrote in message
news:cl******** **@gist.usc.edu ...
Hi,

I need to read a file programmaticall y 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 #4

"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 programmaticall y 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 #5
DJP wrote:
Hi,

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


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

If this seems off-topic in your newsgroup I apologize to comp.lang.c++,
comp.protocols. tcp-ip, comp.unix.progr ammer, and comp.unix.solar is, 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 #6
"DJP" <do***********@ rediffmail.com> writes:
Hi,

I need to read a file programmaticall y 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(inf p)) {
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 #7
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.e du...
I need to read a file programmaticall y 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 #8

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

Similar topics

4
10312
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 sure that there was a way around it but it escapes me if there is one. Thanks Charles Erwin
5
7247
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, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
16
796
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
9992
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 the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer. So two questions: 1. Why did the folks who wrote fgets() have a successful
32
3901
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 default_buffer_length is changed from 4 --24 code works fine. But on the same line if I change the value of default_buffer_length from 4 --10 and I get a memory error. And if the change the value of the same variable from 4 --1024 bytes;
42
6826
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 from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." My question is that if it stops at a new line character (LF?) then how does one read a file with...
9
3424
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
8947
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/ specs gcc version 2.95.3 20010315 (release)
14
2447
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' parameter and set EOF indicator for the stream ? Or will it return the string argument and set EOF indicator only on subsequent call ? Kindly clarify
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.