How to use read() to read data until the end of the file?

前端 未结 3 2224
不知归路
不知归路 2020-12-17 19:41

I\'m trying to read binary data in a C program with read() but EOF test doesn\'t work. Instead it keeps running forever reading the last bit of the file.

#in         


        
3条回答
  •  粉色の甜心
    2020-12-17 20:05

    You must check for errors. On some (common) errors you want to call read again!

    If read() returns -1 you have to check errno for the error code. If errno equals either EAGAIN or EINTR, you want to restart the read() call, without using its (incomplete) returned values. (On other errors, you maybe want to exit the program with the appropriate error message (from strerror))

    Example: a wrapper called xread() from git's source code

提交回复
热议问题