Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/r/read.html on line 1
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/r/read.html on line 1
Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/r/read.html on line 8
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/r/read.html on line 8
Read bytes from a file
#include <unistd.h>
ssize_t read( int fildes,
void* buf,
size_t nbyte );
- fildes
- The descriptor of the file that you want to read from.
- buf
- A pointer to a buffer where the function can store the data that it reads.
- nbyte
- The number of bytes that you want to read.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
The read() function attempts to read nbyte bytes from
the file associated with the open file descriptor, fildes,
into the buffer pointed to by buf.
If nbyte is zero, read() returns zero, and has no other
effect.
On a regular file or other file capable of seeking, read()
starts at a position in the file given by the file offset associated
with fildes. Before successfully returning from
read(), the file offset is incremented by the number of bytes
actually read.
|
The read() call ignores advisory locks that you may have
set with
fcntl(). |
On a file not capable of seeking, read() starts at the current
position.
When read() returns successfully, its return value is the
number of bytes actually read and placed in the buffer. This number
will never be greater than nbyte, although it may be less
than nbyte for one of the following reasons:
- The number of bytes left in the file is less than nbyte.
- The read() request was interrupted by a signal.
- The file is a pipe (or FIFO) or a special file, and has
fewer than nbyte bytes immediately available for reading. For
example, reading from a file associated with a terminal may return one
typed line of data.
If read() is interrupted by a signal before it reads any data,
it returns a value of -1 and sets
errno
to
EINTR. However, if read() is interrupted by a signal
after it has successfully read some data, it returns the number of
bytes read.
No data is transferred past the current end-of-file. If the starting
position is at or after the end-of-file, read() returns zero.
If the file is a device special file, the result of subsequent calls
to read() will work, based on the then current state of the
device (that is, the end of file is transitory).
If the value of nbyte is greater than INT_MAX,
read() returns -1 and sets errno to
EINVAL. See <limits.h>.
When attempting to read from an empty pipe or FIFO:
- If no process has the pipe open for writing, read() returns 0
to indicate end-of-file.
- If a process has the pipe open for writing, and O_NONBLOCK
is set, read() returns -1, and errno is set to
EAGAIN.
- If a process has the pipe open for writing, and
O_NONBLOCK
is clear, read() blocks until some data is written, or the pipe
is closed by all processes that had opened it for writing.
When attempting to read from a file (other than a pipe or FIFO) that
support nonblocking reads and has no data currently available:
- If O_NONBLOCK is set, read() returns -1, and
errno is set to EAGAIN.
- If O_NONBLOCK is clear, read() blocks until
some data is available.
- The O_NONBLOCK flag has no effect if some data is
available.
If you call read() on a portion of a file, prior to the
end-of-file, that hasn't been written, it returns bytes with the
value zero.
If read() succeeds, the st_atime field of the file is
marked for update.
The number of bytes actually read, or -1
(errno is set).
- EAGAIN
- The O_NONBLOCK flag is set for the file descriptor, and
the process would be delayed in the read operation.
- EBADF
- The file descriptor, fildes, isn't a valid file descriptor
open for reading.
- EINTR
- The read operation was interrupted by a signal, and either no data was
transferred, or the resource manager responsible for that file doesn't
report partial transfers.
- EIO
- A physical I/O error occurred (for example, a bad block on a disk). The
precise meaning is device-dependent.
- ENOSYS
- The read() function isn't implemented for the filesystem specified
by filedes.
- EOVERFLOW
- The file is a regular file and an attempt is made to read at or
beyond the offset maximum associated with the file.
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
int fd;
int size_read;
char buffer[80];
/* Open a file for input */
fd = open( "myfile.dat", O_RDONLY );
/* Read the text */
size_read = read( fd, buffer,
sizeof( buffer ) );
/* Test for error */
if( size_read == -1 ) {
perror( "Error reading myfile.dat" );
return EXIT_FAILURE;
}
/* Close the file */
close( fd );
return EXIT_SUCCESS;
}
POSIX 1003.1 XSI
Safety: | |
Cancellation point |
Yes |
Interrupt handler |
No |
Signal handler |
Yes |
Thread |
Yes |
close(),
creat(),
dup(),
dup2(),
errno,
fcntl(),
lseek(),
open(),
pipe(),
readblock(),
readv(),
select(),
write(),
writev()
Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/r/read.html on line 324
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/r/read.html on line 324