Python EOF for multi byte requests of file.read()

后端 未结 2 1287
天命终不由人
天命终不由人 2020-12-16 19:23

The Python docs on file.read() state that An empty string is returned when EOF is encountered immediately. The documentation further states:

2条回答
  •  囚心锁ツ
    2020-12-16 20:20

    Here's what my C compiler's documentation says for the fread() function:

    size_t fread( 
       void *buffer,
       size_t size,
       size_t count,
       FILE *stream 
    );
    

    fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count.

    So it looks like getting less than size means either an error has occurred or EOF has been reached -- so breaking out of the loop would be the correct thing to do.

提交回复
热议问题