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); 7 10275
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
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
"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
"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
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.
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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);
|
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
|
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;
| |
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...
|
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;
|
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)
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |