Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to find an end of file

Reply
Thread Tools

How to find an end of file

 
 
junky_fellow
Guest
Posts: n/a
 
      12-08-2004
What is the proper way of finding an end of file condition while reading
a file ?

How does feof() detects that end of file is reached ?
Does this require support from OS ?

Thanx in advance for any help ...
 
Reply With Quote
 
 
 
 
Charlie Gordon
Guest
Posts: n/a
 
      12-08-2004
"junky_fellow" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> What is the proper way of finding an end of file condition while reading
> a file ?
>
> How does feof() detects that end of file is reached ?


feof() does not detect that end of file is reached, it reports that end of file
was reached during a previous read operation.

> Does this require support from OS ?


if files are implemented with the OS, feof() will rely on the OS reporting such
a condition for read operations.
if files are implemented within the C library, the OS is not an issue.

It is important to notice that feof() is counterintuitive:
- Whether a read operation that reaches the end of a file set the condition is
not specified : fgets() reading the last line will probably not set it, while
fscanf() eating trailing white space might.
- If the file is being written by another function (or process where that makes
sense), feof() may return non 0 even after more data becomes available for
reading (clearerr() or fseek() may be needed before reading).

As a rule of thumb, only use feof() to disambiguate end of file from read error
after a read operation fails.
Idioms such as while (!feof(f)) { ... } and do { ... } while (!feof(f)); are
bogus and must be avoided.

--
Chqrlie.

PS: I know about 7.19.6.2 example 3 verse 19. This code snipplet is bogus and
useless. AAMOF most do/while loops are bogus anyway.


 
Reply With Quote
 
 
 
 
Johnathan Doe
Guest
Posts: n/a
 
      12-08-2004
junky_fellow wrote:
> What is the proper way of finding an end of file condition while reading
> a file ?
>
> How does feof() detects that end of file is reached ?
> Does this require support from OS ?


You can do it yourself without feof(), by doing

struct stat statstruct;

stat("filename", &statstruct);

statstruct.st_size is the number of bytes in the file.

> Thanx in advance for any help ...


Using feof() is easier though.

FILE *fp = fopen(...);

while (!feof(fp)) {
... read stuff
}
 
Reply With Quote
 
Johnathan Doe
Guest
Posts: n/a
 
      12-08-2004
> Using feof() is easier though.
>
> FILE *fp = fopen(...);
>
> while (!feof(fp)) {
> ... read stuff
> }


And of course as the previous poster said, you may have a short count or
even an error, so take that into account.

while (!feof(fp)) {

/* Short count ? */
n = fread(buffer, ..., fp);

if (n != whatever) {
if (ferror())
... some kind of error
if (feof())
... end of file
}
}
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      12-08-2004
Johnathan Doe <No-spam-here-johnathan_doe@!!!nospamthanks!!!fastmail.com.au> scribbled the following:
> junky_fellow wrote:
>> What is the proper way of finding an end of file condition while reading
>> a file ?
>>
>> How does feof() detects that end of file is reached ?
>> Does this require support from OS ?


> You can do it yourself without feof(), by doing


> struct stat statstruct;


> stat("filename", &statstruct);


> statstruct.st_size is the number of bytes in the file.


No you can't. There is no struct stat or stat() in the C language.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
- Karri Kalpio
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      12-08-2004
"Johnathan Doe" <No-spam-here-johnathan_doe@!!!NOSPAMTHANKS!!!fastmail.com.au>
wrote in message
news:41b6c124$0$25761$(E-Mail Removed)...
> > Using feof() is easier though.
> >
> > FILE *fp = fopen(...);
> >
> > while (!feof(fp)) {
> > ... read stuff
> > }

>
> And of course as the previous poster said, you may have a short count or
> even an error, so take that into account.


The previous poster said to avoid this stupid bogus idiom !

> while (!feof(fp)) {
>
> /* Short count ? */
> n = fread(buffer, ..., fp);
>
> if (n != whatever) {
> if (ferror())
> ... some kind of error
> if (feof())
> ... end of file
> }
> }


Instead do this :

for (; {
n = fread(buffer, sizeof(type), count, fp);
if (n != count) {
... handle partial read.
if (ferror(f)) {
... handle error and break or return
break;
}
if (feof(f)) {
.. handle end of file and break or return
break;
}
} else {
... handle normal read
}
}

--
Chqrlie.



 
Reply With Quote
 
Johnathan Doe
Guest
Posts: n/a
 
      12-08-2004
Joona I Palaste wrote:
> No you can't. There is no struct stat or stat() in the C language.


Assuming you're working on 99% of OSes out there, yes you can.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      12-08-2004
Johnathan Doe <No-spam-here-johnathan_doe@!!!nospamthanks!!!fastmail.com.au> scribbled the following:
> Joona I Palaste wrote:
>> No you can't. There is no struct stat or stat() in the C language.


> Assuming you're working on 99% of OSes out there, yes you can.


Here we discuss 100% of the OSes out there.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-08-2004
Johnathan Doe wrote:
>
> Joona I Palaste wrote:
> > No you can't. There is no struct stat or stat() in the C language.

>
> Assuming you're working on 99% of OSes out there, yes you can.


Read the welcome message. This group deals with the language as
defined by the ISO standards, not some imaginary uncontrolled
offshoot. Reference to non-standard features is off-topic. Also
use of feof for any purpose other than to disambiguae a read error
is almost always an error. You would know all this is you had
bothered to read this group for a period before breaking in with
misinformation.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
MarcSmith
Guest
Posts: n/a
 
      12-08-2004

cuz he is using a file pointer can he seek using fseek to the end of th
file, and use ftell to tell him the filesize.

for example:
i assume cuz he is using fgets, feof...ftell and fseek are defined. o
they should be defined.


Code
-------------------


long filesize( FILE *fp )
{
if (fp==NULL) return 0L;
fseek( fp, 0, SEEK_END );
return ftell(fp);
}

-------------------


-
MarcSmit
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 13 09-21-2011 12:17 AM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments