Read, write, and lseek

Source: Internet
Author: User

1. Read Function

1) function prototype:

# Include <unistd. h>

Ssize_t read (int fd, void * Buf, size_t count );

2) function:

The read system calls to read count bytes from the file indicated by the file descriptor FD to the Buf.

3) parameter description:

FD: file descriptor

Buf: stores the cache of read information.

Count: number of bytes to read

Returned value: If the read operation succeeds, the number of bytes read is returned. If the read operation reaches the end, 0 is returned. An error occurs.

Return-1

2. Write function

1) function prototype:

# Include <unistd. h>

Ssize_t write (int fd, const void * Buf, size_t count );

2) function:

The write system calls to write the Count bytes of the buffer to which the Buf points to the file to which the FD points.

3) parameter description:

FD: file to be written

Buf: cache of the information to be written

Count: number of bytes to write

Returned value: If the write operation is successful, the number of written bytes is returned. If an error occurs, the value-1 is returned.

 

3. lseek Function

1) function prototype:

# Include <sys/types. h>

# Include <unistd. h>

Off_t lseek (INT Fildes, off_t offset, int whence );

2) function:

The lseek system call is used to move the read/write pointer position.

3) parameter description:

FD: file to be operated

Offset: the number of shifts relative to whence. Negative values are allowed.

Whence: Start pointer, which has three values

Seek_set calculates the offset from the beginning of the file

Seek_cur calculates the offset from the current position of the file pointer.

Seek_end calculates the offset from the end of the file.

The file pointer value is equal to the current pointer value plus the offset value.

Returned value: when the call is successful, the current read/write location, that is, the number of bytes from the start of the file. If

An error is returned.-1 is returned.

4) common usage:

Move the file read/write pointer to the beginning of the file:

Lseek (INT Fildes, 0, seek_set );

Move the file read/write pointer to the end of the file:

Lseek (INT Fildes, 0, seek_end );

Obtains the current position of the file read/write pointer.

Lseek (INT fikdes, 0, seek_cur );

Note: Some devices (or device files) cannot use lseek. Linux does not allow lseek () to operate on TTY devices. This operation causes the espipe error code within the range of lseek ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.