简体   繁体   English

为什么 'write()' 使用 const 缓冲区,而 'read()' 没有?

[英]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 * ?为什么read()也不采用const void * Does that mean it changes the value of buf ?这是否意味着它改变了buf的值?

Because write() won't change the contents of the buffer, but only write it to the file.因为write()不会更改缓冲区的内容,而只会将其写入文件。

On the other hand, read() will change its buffer (from what's in the file) so it had better not be const .另一方面, read()将更改其缓冲区(从文件中的内容),因此最好不要是const

General rule, what changes isn't const .一般规则,改变的不是const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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