473,657 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

feof usage

My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


nethlek
Nov 13 '05 #1
28 11532
Mac
On Fri, 19 Sep 2003 19:11:20 +0000, Mantorok Redgormor wrote:

My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


The issue is that feof() only tells you AFTER you have attempted to read
past the end of the file. If you understand that, and code accordingly, it
is OK to use it, AFAIK.

That is, if a file has n characters, you can read all of them with fgets
and feof() will still return zero (false). But if you try to read n+1,
then fgets will still succeed, but feof() will return non-zero (true).

HTH

Mac
--
Nov 13 '05 #2
Mantorok Redgormor wrote:
My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?

It's OK -- but feof() only returns a non-zero (`true') value *after*
an attempted `read' has failed.

HTH,
--ag
--
Artie Gold -- Austin, Texas

Nov 13 '05 #3
On 19 Sep 2003 19:11:20 -0700, ne*****@tokyo.c om (Mantorok Redgormor)
wrote in comp.lang.c:
My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


This is covered in the FAQ, specifically at
http://www.eskimo.com/~scs/C-faq/q12.2.html

Generally it is not a good idea to use feof() this way. It is not so
bad if you test for EOF after an input operation and before trying to
use the input data. But it can still cause problems, because an error
does not cause feof() to return a non-zero value.

There are all kinds of things that can cause an error in a file
operation. Hard drives are pretty reliable these days, but they are
still wear parts. Or a user could remove the media containing a file,
such as a floppy disk or CD. Or the network connection could go down.

All functions that can read from a file return a value that indicates
whether they succeeded or failed. fgets() returns the pointer you
passed it if it succeeded, or a null pointer if it failed. So to use
fgets() in a loop:

while (NULL != fgets(buffer, sizeof buffer, file_ptr))
{
/* ... */
}

After your input function indicates a failure, you can use feof() and
ferror() to determine whether you successfully read the entire file or
whether an error occurred.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #4
Mantorok Redgormor wrote:
My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


Sure, it's OK. But it is useless until EOF has already been reached, by
which time whatever you are using to read the file (fgets, fgetc, fread,
even fscanf) has already given an error indication. feof can be used to
sort out what kind of input error it was. You don't reach EOF on a read
that completely succeeds, so feof is useless then.
--
Martin Ambuhl

Nov 13 '05 #5

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


Most users of feof() have previously used Pascal, where it is required to
use feof() before reading to avoid a fatal error.

C requires an attempt to read past the end of file before feof() will
indicate it, so direct translation of Pascal feof() to C's feof() tend to
fail.

All C input functions signal the failure to do what they were asked to do,
and that tends to be a better way to do the test.

-- glen
Nov 13 '05 #6
Mac
On Sat, 20 Sep 2003 04:16:09 +0000, Martin Ambuhl wrote:
Mantorok Redgormor wrote:
My professor that teaches C says that using feof to test if EOF is
reached while using a function like fgets on a file is okay. but i've
been told elsewhere that it is not okay. what is the case here? is it
okay or not?


Sure, it's OK. But it is useless until EOF has already been reached, by
which time whatever you are using to read the file (fgets, fgetc, fread,
even fscanf) has already given an error indication. feof can be used to
sort out what kind of input error it was. You don't reach EOF on a read
that completely succeeds, so feof is useless then.


Actually, fgets doesn't provide error notification on reaching the end of
a file unless it wasn't able to read any characters, in which case it
returns NULL. It also returns NULL if there is an error reading from the
file.

Apart from that I agree with you. It seems that feof() is seldom useful,
except to distinguish between end of file and some other file error after
the fact.

Mac
--
Nov 13 '05 #7
Mac wrote:
On Fri, 19 Sep 2003 19:11:20 +0000, Mantorok Redgormor wrote:
My professor that teaches C says that using feof to test if EOF
is reached while using a function like fgets on a file is okay.
but i've been told elsewhere that it is not okay. what is the
case here? is it okay or not?


The issue is that feof() only tells you AFTER you have attempted
to read past the end of the file. If you understand that, and
code accordingly, it is OK to use it, AFAIK.

That is, if a file has n characters, you can read all of them with
fgets and feof() will still return zero (false). But if you try to
read n+1, then fgets will still succeed, but feof() will return
non-zero (true).


Correction - that final fgets call will not succeed, it will
return NULL. This may describe either the end-of-file, or a read
error. feof() allows you to disambiguate them.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #8
Glen Herrmannsfeldt wrote:
"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
My professor that teaches C says that using feof to test if EOF
is reached while using a function like fgets on a file is okay.
but i've been told elsewhere that it is not okay. what is the
case here? is it okay or not?


Most users of feof() have previously used Pascal, where it is
required to use feof() before reading to avoid a fatal error.

C requires an attempt to read past the end of file before feof()
will indicate it, so direct translation of Pascal feof() to C's
feof() tend to fail.

All C input functions signal the failure to do what they were
asked to do, and that tends to be a better way to do the test.


The reason for the difference is that Pascal input is always
buffered, so that a look ahead can be implemented by examining
f^. In C, such lookahead involves the use of ungetc() and the
user needs to be highly aware of what is going on. In Pascal
OTOH use of the buffering can cause problems with interactive
input, leading to so-called lazy-io schemes, which in turn
require the user to be aware when using eof and eoln calls.

Neither scheme is perfect, but the trade-offs are different.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #9

"LibraryUse r" <de**********@m ade.invalid> wrote in message
news:3F******** *******@made.in valid...
Glen Herrmannsfeldt wrote:
"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
My professor that teaches C says that using feof to test if EOF
is reached while using a function like fgets on a file is okay.
but i've been told elsewhere that it is not okay. what is the
case here? is it okay or not?
Most users of feof() have previously used Pascal, where it is
required to use feof() before reading to avoid a fatal error.

C requires an attempt to read past the end of file before feof()
will indicate it, so direct translation of Pascal feof() to C's
feof() tend to fail.

All C input functions signal the failure to do what they were
asked to do, and that tends to be a better way to do the test.


The reason for the difference is that Pascal input is always
buffered, so that a look ahead can be implemented by examining
f^. In C, such lookahead involves the use of ungetc() and the
user needs to be highly aware of what is going on. In Pascal
OTOH use of the buffering can cause problems with interactive
input, leading to so-called lazy-io schemes, which in turn
require the user to be aware when using eof and eoln calls.


Interactive input can be confusing in C, too.
Neither scheme is perfect, but the trade-offs are different.


Using the assumptions of one language in programming the other tends to show
those trade-offs.

-- glen
Nov 13 '05 #10

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

Similar topics

7
2088
by: Wolfgang Werners-Lucchini | last post by:
Hallo! The code here outputs '0'. Why? ---------------------------- $filename = "test.dat"; if (!file_exists($filename)) { touch($filename); // Create blank file chmod($filename,0666); } $fp=fopen($filename,'r');
11
5930
by: | last post by:
Hi all...this is a great forum, In one of my posts, someone tell me that is more secure use input function with 'A field-width specifier'... So my question, which function i should use ?. "scanf("%s",start->acNome);" >>guarantee for hackers to kontaminate your mashine with >>viruses of any kind. Don't use scanf or buffer overflow >>ruins your mashine.
8
14389
by: rCs | last post by:
Which of the following two approaches is preferred (and why)? int c; do { ... c = getchar(); } while (c != EOF); - or -
12
2415
by: Sankar | last post by:
Dear all, I am programming in Linux , wherein I need to know a couple of things. 1) Does an API exist that can copy file onto another file ( an API equivalent of 'cp') ? 2) Is there an API that tells me the disk usage ( equivalent for cmdline
20
7522
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file frequently(forwards and backwards) and do fread() from that offset. Or better still, could anyone let me know some good ways to achieve what I need to do as above?Can I get hold of the file and being able to read in without using fread()? Using...
24
5992
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h> #include<string.h> void main(){ FILE *fp,*ftemp;
0
8425
marktang
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...
0
8845
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
8743
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...
1
8522
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,...
0
8622
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
7355
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
6177
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
5647
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
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.