文章目錄
- fseek, _fseeki64
- 作用
- 頭檔案
- 函數原型
- 參數
- 傳回值
- 備注
- 代碼示例
fseek, _fseeki64
作用
将檔案指針移到指定位置。
頭檔案
fseek <stdio.h>
_fseeki64 <stdio.h>
函數原型
int fseek(
FILE *stream,
long offset,
int origin
);
int _fseeki64(
FILE *stream,
__int64 offset,
int origin
);
參數
-
stream
指向 FILE 結構的指針。
-
offset
中的位元組數 origin 。
-
origin
初始位置。
自變量 origin 必須是下列常量之一,在中定義 stdio.h :
原始值 | 含義 |
SEEK_CUR | 檔案指針的目前位置。 |
SEEK_END | 檔案結尾。 |
SEEK_SET | 檔案開頭。 |
傳回值
如果成功, fseek 則 _fseeki64 傳回0。
否則,傳回一個非零值。
在無法查找的裝置上,傳回值是未定義的。 如果 stream 為 null 指針,或者如果不 origin 是下面所述的允許值之一, fseek 則 _fseeki64 調用無效參數處理程式,如 參數驗證中所述。 如果允許執行繼續,則這些函數将設定 errno 為 EINVAL 并傳回-1。
備注
代碼示例

檔案中
// crt_fseek.c
// This program opens the file FSEEK.OUT and
// moves the pointer to the file's beginning.
#include <stdio.h>
int main( void )
{
FILE *stream;
char line[81];
int result;
if ( fopen_s( &stream, "fseek.out", "w+" ) != 0 )
{
printf( "The file fseek.out was not opened\n" );
return -1;
}
fprintf( stream, "The fseek begins here: "
"This is the file 'fseek.out'.\n" );
result = fseek( stream, 23L, SEEK_SET);
if( result )
perror( "Fseek failed" );
else
{
printf( "File pointer is set to middle of first line.\n" );
fgets( line, 80, stream );
printf( "%s", line );
}
fclose( stream );
}