ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.)
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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 ...
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.
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
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.