1. File hole
The file's offset can be greater than the file's current size, in which case the next write to the file will extend the file. This is referred to as creating a hole in a file and is allowed. Any bytes in a file that not been written are read back as 0.
A hole in a file isn't required to have storage backing it on disk. Depending on the file system implementation, when you write after seeking past the end of the file, new disk blocks might be allocated to store the data, but there's no need to allocate disk blocks for the data between the old end of file and t he location where you start writing.
2. read Function
#include <unistd.h>
ssize_t read(int filedes, void *buf, size_t nbytes);
// Returns: number of bytes read, 0 if end of file, -1 on error
read讀取的字節數小于所要求的字節數的幾種可能:
1) 從文件中讀取,在所要求的字節數讀取完成前到達文件尾。
2) 從終端讀取,這種情況下通常每次最多讀取一行內容。
3) 通過網絡讀取,網絡緩沖可能導致讀取到少于要求的字節數。
4) 從管道或者FIFO中讀取
5) 從record-oriented設備中讀取,如磁帶,每次至多返回一個記錄。orz...
6) 在一定數量的數據讀取后被信號中斷。
3. write Function
#include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes);
// Returns: number of bytes written if OK, -1 on error
導致錯誤的通常原因是磁盤已滿,或者超出了給定進程的文件大小限制。
The file's offset can be greater than the file's current size, in which case the next write to the file will extend the file. This is referred to as creating a hole in a file and is allowed. Any bytes in a file that not been written are read back as 0.
A hole in a file isn't required to have storage backing it on disk. Depending on the file system implementation, when you write after seeking past the end of the file, new disk blocks might be allocated to store the data, but there's no need to allocate disk blocks for the data between the old end of file and t he location where you start writing.
2. read Function
#include <unistd.h>
ssize_t read(int filedes, void *buf, size_t nbytes);
// Returns: number of bytes read, 0 if end of file, -1 on error
read讀取的字節數小于所要求的字節數的幾種可能:
1) 從文件中讀取,在所要求的字節數讀取完成前到達文件尾。
2) 從終端讀取,這種情況下通常每次最多讀取一行內容。
3) 通過網絡讀取,網絡緩沖可能導致讀取到少于要求的字節數。
4) 從管道或者FIFO中讀取
5) 從record-oriented設備中讀取,如磁帶,每次至多返回一個記錄。orz...
6) 在一定數量的數據讀取后被信號中斷。
3. write Function
#include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes);
// Returns: number of bytes written if OK, -1 on error
導致錯誤的通常原因是磁盤已滿,或者超出了給定進程的文件大小限制。