简体   繁体   中英

Why does 'write()' take a const buffer while 'read()' doesn't?

The signatures for read / write are below:

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

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

Why doesn't read() also take a const void * ? Does that mean it changes the value of buf ?

Because write() won't change the contents of the buffer, but only write it to the file.

On the other hand, read() will change its buffer (from what's in the file) so it had better not be const .

General rule, what changes isn't const .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM