C++ Reference: Standard C++ Library reference: C Library: cstdio: fgetpos

C++官网参考链接:https://cplusplus.com/reference/cstdio/fgetpos/

函数 

fgetpos
int fgetpos ( FILE * stream, fpos_t * pos );
获取流中的当前位置
获取stream中的当前位置。
该函数用stream的位置指示符中所需的信息填充pos指向的fpos_t对象,通过调用fsetpos以将stream恢复到当前位置(如果是面向宽字符的,则是多字节状态)。
ftell函数可用于以整数值的形式获取stream中的当前位置。

形参
stream 
指向标识流的FILE对象的指针。
pos
指向fpos_t对象的指针。
这应该指向一个已经分配的对象。

返回值
如果成功,函数返回0。
如果出现错误,errno将被设置为平台特定的正数值,函数将返回一个非0值。

用例
/* fgetpos example */
#include
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):

C++ Reference: Standard C++ Library reference: C Library: cstdio: fgetpos_第1张图片

该示例打开myfile.txt,然后读取第一个字符一次,然后读取相同的第二个字符3次。 

C++ Reference: Standard C++ Library reference: C Library: cstdio: fgetpos_第2张图片

另请参考
fsetpos    Set position indicator of stream (function)
ftell    Get current position in stream (function)
fseek    Reposition stream position indicator (function) 

你可能感兴趣的:(C++,Reference,C,Library,c++,c语言,获取流中当前位置,fgetpos)