LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2004, 09:28 AM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
finding the eof


Another descriptor question... Is there any function for finding out if Ive reached the end of file? ie. An equivalent of feof()
 
Old 11-25-2004, 09:45 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
what do you want eof for?
when you read a file,
in most cases you just do

Code:
c = getc(fd);
while( c != EOF)
     c=getc(fd);
or someting like that.
be more specific, give an example
 
Old 11-25-2004, 09:51 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Feof is clearly a library call that has no back-end system call equivalent.
It just tell if a previous read has failed because of a EOF condition.
 
Old 11-25-2004, 09:52 AM   #4
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
nope, im converting some code from using file pointers to use unix descriptors instead, and since theres no feof() equivalent for using with descriptors (that I can find) , Im wondering whats the easiest way to mimic the behaviour of feof. As far as I can see the only way is using the read() function and checking if it returns the same amount of bytes, but I KNOW theres a more proper way to do it
 
Old 11-25-2004, 10:36 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
stdio's feof() only returns true if the last fgets(), fread(), or whatever has already hit the end-of-file.

Of course you can do the same with read() from a filedescriptor.
According to "man 2 read":
Quote:
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file) [...]
So you can easily implement your own feof() (use a different name though, let's say "f_eof()" ) for file descriptors just by checking if a dummy read() returns 0:
Code:
#include <unistd.h>

int f_eof(int fd)
{
    int dummy_buf;
    return (read(fd, &dummy_buf, 1) == 0);
}
I'm quite sure there's no other way, not really different anyway.

P.S. Happy birthday
 
Old 11-25-2004, 10:37 AM   #6
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
As far as I can see the only way is using the read() function and checking if it returns the same amount of bytes, but I KNOW theres a more proper way to do it
in the next read after u reach the end of file, read will return -1 which is the error for EOF.(You probably know that). This is what the stream functions check, since fopen,fclose,feof, etc are all constucted using basic I/O functions (read,write,open etc.)
 
Old 11-25-2004, 10:40 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by perfect_circle
in the next read after u reach the end of file, read will return -1 which is the error for EOF.
No.
If read() returns -1 it's a real error.
read() returns 0 on EOF.
 
Old 11-25-2004, 11:08 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
By the way, in case of no EOF condition, reading one byte would hide it from the next read calls, don't forget to store it somewhere in your library and bring it back to the next read ...

Quote:
but I KNOW theres a more proper way to do it
If you KNOW, don't ask, tell us !

Frankly speaking, I'm afraid you're trying to reinvent the wheel, and that you'll be disappointed by the performance gain, you'll reach, if any ...
 
Old 11-25-2004, 11:18 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by jlliagre
By the way, in case of no EOF condition, reading one byte would hide it from the next read calls, don't forget to store it somewhere in your library and bring it back to the next read ...
Oops, I forgot that part.
Thanks for catching that.
 
Old 11-25-2004, 01:01 PM   #10
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
Quote:
If you KNOW, don't ask, tell us !

Frankly speaking, I'm afraid you're trying to reinvent the wheel, and that you'll be disappointed by the performance gain, you'll reach, if any ...
Alright I get the message! - BTW there was no performance gain but a big performance drop. Still I feel its more standard this way and therefore better for porting.

I can always make the code more efficent when it actually becomes something important to me
 
Old 11-25-2004, 06:52 PM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by nodger
Alright I get the message! - BTW there was no performance gain but a big performance drop.
How big are the buffers you're read()ing data into?
 
Old 11-25-2004, 09:42 PM   #12
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Code:
No.
If read() returns -1 it's a real error.
read() returns 0 on EOF.
That's true...sorrrrrrrrryyyyyyyy!!!
Quote:
Alright I get the message! - BTW there was no performance gain but a big performance drop. Still I feel its more standard this way and therefore better for porting.
in C streams are used for many many years (more than 20), I would agree with jlliagre. What you are trying to do is pointless.

Last edited by perfect_circle; 11-25-2004 at 10:30 PM.
 
Old 11-26-2004, 02:17 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
in C streams are used for many many years (more than 20)
Correct, precisely 25 years.
Here's an excerpt of the venerable Unix Version 7 manual, dated January, 1979.
Quote:
Standard I/O.
The old fopen, getc, putc complex and the old -lp package are both dead, and even
getchar has changed. All have been replaced by the clean, highly efficient, stdio(3) package. The first
things to know are that getchar(3) returns the integer EOF (-1), which is not a possible byte value, on
end of file, that 518-byte buffers are out, and that there is a defined FILE data type.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
EOF finding Goblin_C_Noob Programming 7 09-10-2005 09:58 AM
Premature EOF? miknight Programming 1 04-04-2004 12:19 AM
eof ... c ... linux xviddivxoggmp3 Programming 6 03-31-2004 12:56 PM
Question about EOF (C) xailer Programming 6 12-07-2003 11:15 AM
<< Eof ? sikandar Linux - Software 5 09-18-2003 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration