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
          導致錯誤的通常原因是磁盤已滿,或者超出了給定進程的文件大小限制。
          主站蜘蛛池模板: 曲阳县| 仁怀市| 朝阳市| 肥西县| 长阳| 灵璧县| 塔河县| 长治县| 察雅县| 招远市| 灵山县| 万山特区| 陇西县| 西盟| 隆林| 望江县| 忻城县| 那曲县| 公主岭市| 霍山县| 阿鲁科尔沁旗| 故城县| 恩平市| 武隆县| 崇仁县| 金坛市| 临夏市| 贵港市| 北宁市| 安西县| 安福县| 布尔津县| 嵩明县| 仙游县| 鹤庆县| 漠河县| 湖北省| 突泉县| 定陶县| 曲阳县| 建昌县|