C++官网参考链接:https://cplusplus.com/reference/cstdio/fgetpos/
函数
<cstdio>
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):

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