C庫函數 int fseek(FILE *stream, long int offset, int whence) 設置流的檔位置給定的偏移量。
聲明
以下是 fseek() 函數的聲明。
int fseek(FILE *stream, long int offset, int whence)
參數
-
stream -- 這是一個檔對象的標識流的指針。
-
offset -- 這是,以抵消 where 的位元組數。
-
whence -- 這是位置偏移量添加。它指定由下列常數之一:
Constant | 描述 |
---|---|
SEEK_SET | Beginning of file |
SEEK_CUR | Current position of the file zaixianer |
SEEK_END | End of file |
返回值
如果成功,這個函數返回零,否則返回非零值。
例子
下麵的例子演示了如何使用fseek()函數。
#include <stdio.h> int main () { FILE *fp; fp = fopen("file.txt","w+"); fputs("This is xuhuhu.com", fp); fseek( fp, 7, SEEK_SET ); fputs(" C Programming Langauge", fp); fclose(fp); return(0); }
讓我們編譯和運行上面的程式,這將創建一個包含以下內容的檔file.txt 。最初程式創建檔,並寫道: This is xuhuhu.com 但後來我們寫指針複位在第七的位置,從一開始使用 puts() 語句寫檔包含以下內容:
This is C Programming Langauge
上一篇:
freopen() - C語言庫函數
下一篇:
fsetpos() - C語言庫函數