c语言中read函数的返回值

$ man 2 read 查看手册如下:

     #include 
     #include 
     #include 

     ssize_t
     pread(int d, void *buf, size_t nbyte, off_t offset);

     ssize_t
     read(int fildes, void *buf, size_t nbyte);

     ssize_t
     readv(int d, const struct iovec *iov, int iovcnt);

RETURN VALUES
     If successful, the number of bytes actually read is returned.  Upon reading end-of-file, zero is returned.  Otherwise, a -1 is returned and the global variable errno is set to indicate the error.

read函数的返回值为ssize_t, 当有error时, 返回值为-1. 之前一直当返回值为size_t, 只判断了end-of-file的情况, 忽略了error的处理, 所以给线上环境造成一定的影响. code review还是挺管用的, 今天同事一眼就看出来了.

你可能感兴趣的:(c语言中read函数的返回值)