檔案操作 fseek函式和ftell函式

2021-10-19 21:51:54 字數 547 閱讀 2023

1、fseek函式:

int fseek(file * _file, long _offset, int _origin);
函式設定檔案指標stream的位置。如果執行成功,stream將指向以fromwhere為基準,偏移offset(指標偏移量)個位元組的位置,函式返回0。如果執行失敗則不改變stream指向的位置,函式返回乙個非0值。

超出檔案末尾位置,還是返回0。往回偏移超出首位置,還是返回0,小心使用。

seek_set:檔案開頭

seek_cur:當前位置

seek_end:檔案結尾

fseek(fp, 3, seek_set);

2、ftell函式

ftell函式用於得到檔案位置指標當前位置相對與檔案首的偏移位元組數。在隨機方式訪問檔案時,由於檔案位置頻繁的前後移動,程式不容易確定檔案的當前位置。

long len = ftell(fp)

檔案操作 fseek函式和ftell函式

1 fseek函式 int fseek file file,long offset,int origin 函式設定檔案指標stream的位置。如果執行成功,stream將指向以fromwhere為基準,偏移offset 指標偏移量 個位元組的位置,函式返回0。如果執行失敗則不改變stream指向的位...

檔案操作 fseek

int fseek file stream,long offset,int fromwhere fseek 用於二進位制方式開啟的檔案,移動檔案讀寫指標位置.int fseek file stream,long offset,int origin 第乙個引數stream為檔案指標 第二個引數offs...

檔案操作函式(1) fseek

int fseek file stream,long offset,int origin 第乙個引數stream為檔案 指標 第二個引數offset為 偏移量,正數表示正向偏移,負數表示負向偏移 第三個引數origin設定從檔案的 開始偏移,可能取值為 seek cur seek end 或 see...