#include <unistd.h> #include <stdlib.h> int main() { char buffer[128]; int nread; nread=read(0,buffer,128); if(nread==-1) write(2,"A read error has occurred\n",26); if((write(1,buffer,nread))!=nread) write(2,"A write error has occurred\n",27); exit(0); }
函数原型:
#include <unistd.h>
size_t read(int fildes,const void *buf,size_t nbytes);
read()参数一:同write()
参数二:读取的数据
参数三:读取的数据的大小
返回:
正常情况下返回读取的数据的大小,即nbytes
返回0表示没有读入任何数据,可能达到了文件尾
返回-1表示出错了