サイズ 5 のファイルがあり、lseek を使用して位置 10 にシークし、次に read を使用してこの位置から 1 バイトを読み取ると、read システム コールの戻り値は 0 になります。 lseek/read/ の戻り値を知りたい次の場合に書き込みます。ファイル システムで許可されている最大ファイル サイズを超えてシークし、読み取りを実行した場合。この位置までシークできますか/エラーですか? そうでない場合、ここで読み取りを行うと、読み取りの戻り値は何になりますか? 0 または -1?
1 に答える
The POSIX specs for lseek()
state that you are allowed to set the position beyond the end of the file:
The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file.
It only complains if the resulting position is beyond the bounds of the off_t
variable type returned from lseek()
. There seem to be no error messages specified which indicate things like "beyond your process-specific limit" or "beyond the capacity of the filesystem itself".
However, trying to extend the file after that seek with a write()
can return the EFBIG
error code, again according to POSIX. Best bet would be to actually try this and see what the behaviour is, especially since Linux doesn't necessarily comply with POSIX in all cases - in other words, the behaviour may be different between POSIX-compliant UNIX systems and Linux.
The POSIX read()
specs are quite adamant about what happens:
No data transfer shall occur past the current end-of-file. If the starting position is at or after the end-of-file, 0 shall be returned.
Hence, you should get back zero since, by definition, if you've done an lseek()
beyond the capacity of the filesystem, you're assuredly beyond the end of the file (you can't create files bigger than the filesystem allows).