當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C語言 fgetpos用法及代碼示例

C語言stdio頭文件(stdio.h)中fgetpos函數的用法及代碼示例。

用法:

int fgetpos ( FILE * stream, fpos_t * pos );
獲取信息流中的當前位置
檢索當前位置

該函數填充fpos_t指向的對象位置與所需的信息位置指示器恢複到當前位置(以及多字節狀態,如果wide-oriented)並調用fsetpos

這個ftell函數可用於檢索當前位置作為整數值。

參數

stream
指向一個指針FILE標識流的對象。
pos
指向一個指針fpos_t目的。
這應該指向已經分配的對象。

返回值

成功時,該函數返回零。
如有錯誤,errno設置為特定於平台的正值,並且該函數返回非零值。

示例

/* fgetpos example */
#include <stdio.h>
int main ()
{
   FILE * pFile;
   int c;
   int n;
   fpos_t pos;

   pFile = fopen ("myfile.txt","r");
   if (pFile==NULL) perror ("Error opening file");
   else
   {
     c = fgetc (pFile);
     printf ("1st character is %c\n",c);
     fgetpos (pFile,&pos);
     for (n=0;n<3;n++)
     {
        fsetpos (pFile,&pos);
        c = fgetc (pFile);
        printf ("2nd character is %c\n",c);
     }
     fclose (pFile);
   }
   return 0;
}

可能的輸出(與myfile.txt包含ABC):
1st character is A
2nd character is B
2nd character is B
2nd character is B

該示例打開myfile.txt,然後讀取第一個字符一次,然後讀取相同第二個字符的3倍。

相關用法


注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C fgetpos function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。