函数原型:
#include<unistd.h>
size_t write(int filedes,const char* buf,size_t nbytes);
系统调用write的作用是把缓冲区buf里的前nbytes字节写入与文件描述符filedes相关联的文件中去。它的返回值是实际写出的字节数。如果文件面舒服有错误或者底层设备驱动程序对数据块尺寸比较敏感,该返回值就可能小于nbytes的值。如果这个函数的返回值是0,就表示没有写入任何数据;如果返回时-1,就表示在write调用中出现了错误,对应的错误代码保存在全局变量error里面。
注意:
Write出错误的一个常见错误的原因是:磁盘已写满,或者超过了一个给定进程的文件长度限制。
例:
#include<stdipo.h>
#include<unistd.h>
int main()
{
if((write(1,"Hereis somedata\n",18))!=18)
write(2,"Awriteerror has occurredon filedescriptor 1\n",46);
exit(0);
}
结果:
$ ./a
Hereis somedata