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.
I need help with the read() function in C. I am trying to read a file one character at a time using the open() and read() functions, but I am getting an infinite loop. The open() function I have below returns a "3", meaning the file is opened successfully. However, the read() function returns -1 all the time, sending it into an endless loop when it shoudl be returning the number of bytes read at any given moment until the EOF.
You should definitely check for "-1" in your read call, and you should definitely #include <errno.h> and check the value of "errno". You might also wish to call "perror()" for bona fide error conditions (but not, of course, for run-of-the-mill EOF conditions).
read()
#include <fcntl.h>
int read( int handle, void *buffer, int nbyte );
Can someone please explain the arguments for the read function? What should *buffer be declared as and what is the int handle (I do understand the use of nbyte.)?
1. Allocate a buffer: either "malloc()", or declare some "char myarray[BUFSIZE]" on the stack.
2. Open the file.
Make sure the file is opened successfully.
Print error status (errno and/or perror) if it fails.
3. Read from the file; again checking the return value and again checking error status.
This time, however, you need to distinguish between
a) a successful read (#/bytes read == #/bytes expected)
b) a partial read (#/bytes read < #/bytes expected)
c) EOF (#/bytes == -1, but errno == 0)
d) An I/O error (#/bytes == -1, errno is nonzero)
4. Close the file
STYLE POINT:
I would use the variable name "fd" (integer "file descriptor") for open/read/close, vs the name "fp" (FILE "file pointer"), which I would use for "fopen/fread/fclose".
As far as the arguments: it should be pretty self-explanatory:
- fd: file descriptor (of some file you opened)
- buffer: (array of bytes, or a struct, you allocated)
- nbytes: (the #/bytes you'd like to read (there may or may not be that many bytes available)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.