C ++: read and write Functions

Source: Internet
Author: User

1. read

# Include<Unistd. h>

Ssize_t read (int filedes, void * buf, size_t
Nbytes );
 
Returned value: the number of bytes read; 0 (read EOF);-1 (error)

The read function reads nbytes bytes from the opened file specified by filedes to the buf. In the following cases, the number of bytes read is smaller than that of nbytes.
:

A. When reading A common file, it is not enough nbytes bytes to read the end of the file. For example, if the file is only 30 bytes and we want to read 100 bytes, we only read 30 bytes.
Byte. The read function returns 30. Then, if you use the read function to act on this file, the read will return 0.

B. When reading data from a terminal device, generally only one row can be read at a time.

C. When reading data from the network, the network cache may cause the number of bytes to be read to be smaller than that of nbytes.

D. When reading pipe or FIFO, the number of bytes in pipe or FIFO may be less than nbytes.

E. When reading from a record-oriented device, some record-oriented devices (such as tapes) can only return one record at a time.

F. The signal is interrupted when some data is read.

The read operation starts with cfo. Cfo increases before the result is returned, and the increment is the number of bytes actually read.

2. write

# Include<Unistd. h>

Ssize_t write (int filedes, const void * buf, size_t
Nbytes );
 
Returned value: number of bytes written to the file (successful);-1 (error)

The write function writes nbytes bytes of data to filedes. The data source is buf. The returned value is always equal
Nbytes. Otherwise, an error occurs. A common error occurs when the disk space is full or exceeds the file size limit.

For common files, the write operation starts with cfo. If O_APPEND is used to open a file, data is written to the end of the file in each write operation. Cfo
Increment: the number of bytes actually written.

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.