Linux I/O read-write files

Source: Internet
Author: User

Once the file description is obtained, the file can be read and written.

1. Read the file

ssize_t Read (int fd, void* buf, size_t len);

Parameters:

FD: File descriptor

BUF: Store Read cache

Len: Expected number of bytes read

return value:

The number of bytes actually read.

Describe:

When read is called, the system reads Len bytes from the current location of the file referenced by the FD parameter to BUF. The return value is the number of bytes written to BUF. Execution succeeds, returns the number of bytes read, failure returns-1, and sets Erron.


All possible results of read reads:

①return = = Len. Normal

②0< return < Len. In both cases,

First: The read process is interrupted by a signal, the number of bytes available for reading is greater than 0, which is less than Len, interrupted during reading

Second: EOF was reached before reading to Len bytes.

Re-making the read () call to read the remaining bytes into the remaining buffer.

③return = = 0. Eof

④ is blocked.

⑤return = =-1 && erron = = Eintr. was interrupted before it started reading.

⑥return = =-1 && erron = = Eagain. is blocked and should be called later.

⑦return = =-1 && erron = = Othervalue. Critical Error

Read the File Code demo:

To read Len bytes ssize_t ret;while (len! = 0 && (ret=read (FD, buf, Len)) {if (ret = =-1) {if (!=0 = = Erron) {      Continue      }else if (erron = = Eagain) {sleep (1);       }else{perror ("read error");      Break    }} len-= ret; BUF + = ret;}


2. Write files

ssize_t write (int fd,const void* buf, size_t count);

Parameters:

FD: File descriptor

BUF: Byte buffer to write

Count: Number of bytes to write

return value:

The number of bytes actually written

Describe:

Writes count bytes from BUF to the location of the file pointed to by FD. After successful execution, the file location is updated with it. Write does not, like read, reach the EOF location of the file, except that it does not write to only a subset of bytes.

Read method when partial bytes are returned:

ssize_t ret, nr;while (len! = 0 && (ret = write (FD, buf, len)) = 0) {if (ret = =-1) {if (errno = = eintr) {        Continue        } perror ("write");    Break    } Len-=ret; BUF + = ret;}

To improve efficiency, the kernel buffers data when it writes to disk. Buffer time can be passed by parameter

/proc/sys/vm/dirty_expire_centiseconds to set this value.

Tips

size_t/ssize_t is the data type specified by POSIX. On 32-bit systems, they represent unsigned int/int, respectively.

This article is from the "Rotten Pen" blog, please make sure to keep this source http://cuiyiyi.blog.51cto.com/4080254/1702636

Linux I/O read-write files

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.