Posted on 2007-08-13 22:14
ZelluX 阅读(391)
评论(0) 编辑 收藏 所属分类:
Linux 、
C/C++
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
导致错误的通常原因是磁盘已满,或者超出了给定进程的文件大小限制。