天天看點

c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)

  fseek庫函數

對流stream相關的檔案定位,随後的讀寫操作将從新位置開始。

對于二進制檔案,此位置被定位在由origin開始的offset個字元處。origin的值可能為SEEK_SET(檔案開始處)、SEEK_CUR(目前位置)或SEEK_END(檔案結束處)。

對于文本流,offset心須為0,或者是由函數ftell()傳回的值(此時origin的值必須是SEEK_SET)(這裡關于與ftell函數的互動,不是很了解。)。

ftell庫函數

傳回與流stream相關的檔案的目前位置。出錯時傳回-1L。

fflush庫函數

對輸出流(寫打開),fflush()用于将已寫到緩沖區但尚未寫出的全部資料都寫到檔案中;對輸入流,其結果未定義。如果寫過程中發生錯誤則傳回EOF,正常則傳回0。

fflush(NULL)用于重新整理所有的輸出流。

程式正常結束或緩沖區滿時,緩沖區自動清倉。

 lseek庫函數

 lseek函數不是ANSI C标準庫函數,隻是滿足POSIX的UNIX下的函數。

函數說明:

每一個已打開的檔案都有一個讀寫位置, 當打開檔案時通常其讀寫位置是指向檔案開頭, 若是以附加的方式打開檔案(如O_APPEND), 則讀寫位置會指向檔案尾. 當read()或write()時, 讀寫位置會随之增加,lseek()便是用來控制該檔案的讀寫位置. 參數fildes 為已打開的檔案描述詞, 參數offset 為根據參數whence來移動讀寫位置的位移數.

涉及到的枚舉變量

c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)
c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)

--------------------代碼實作----------------------------

The standard library function 

1

<code>int</code> <code>fseek(FILE*fp,</code><code>long</code> <code>offset,</code><code>int</code> <code>origin)  </code>

is identical to <code>lseek</code> except that <code>fp</code> is a file pointer instead of a file descriptor and the return value is an <code>int</code> status, not a position. Write <code>fseek</code> . Make sure that your <code>fseek</code> coordinates properly with the buffering done for the other functions of the library. 

Here's Gregory's first solution: <code></code>

c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)
c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)

...and here's his second, which is considerably more comprehensive:<code></code>

c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)
c程式設計語言_習題8-4_重新實作c語言的庫函數fseek(FILE*fp,longoffset,intorigin)

本文轉自二郎三郎部落格園部落格,原文連結:http://www.cnblogs.com/haore147/p/3648307.html,如需轉載請自行聯系原作者

繼續閱讀