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

          APUE - File I/O (2)

          Posted on 2007-08-13 22:14 ZelluX 閱讀(396) 評論(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
          導致錯誤的通常原因是磁盤已滿,或者超出了給定進程的文件大小限制。
          主站蜘蛛池模板: 阿拉善右旗| 乌拉特前旗| 新巴尔虎右旗| 古丈县| 平罗县| 炎陵县| 衡阳县| 顺义区| 土默特右旗| 马关县| 凤冈县| 龙海市| 金山区| 罗城| 益阳市| 界首市| 谷城县| 石泉县| 常宁市| 白朗县| 宁津县| 苗栗县| 泉州市| 黄石市| 留坝县| 威宁| 寿宁县| 罗江县| 松潘县| 灵武市| 柞水县| 民乐县| 大渡口区| 天峻县| 潼关县| 英山县| 烟台市| 洮南市| 元氏县| 璧山县| 姜堰市|