posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          APUE - File I/O (2)

          Posted on 2007-08-13 22:14 ZelluX 閱讀(399) 評論(0)  編輯  收藏 所屬分類: LinuxC/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
          導致錯誤的通常原因是磁盤已滿,或者超出了給定進程的文件大小限制。
          主站蜘蛛池模板: 广州市| 乌什县| 青州市| 峨边| 九寨沟县| 西华县| 云梦县| 前郭尔| 苏尼特右旗| 长武县| 集安市| 西华县| 太康县| 台东县| 汶川县| 曲麻莱县| 石景山区| 延庆县| 金川县| 景德镇市| 咸丰县| 永顺县| 寿宁县| 房产| 崇左市| 醴陵市| 香港 | 怀化市| 辛集市| 凌云县| 南丰县| 天镇县| 长武县| 缙云县| 临汾市| 灵山县| 道孚县| 仁怀市| 靖远县| 来凤县| 渑池县|