c语言fsetpos是什么,fsetpos - [ C语言中文开发手册 ] - 在线原生手册 - php中文网

在头文件中定义int fsetpos(FILE * stream,const fpos_t * pos);

stream根据指向的值设置文件流的文件位置指示符和多字节解析状态(如果有)pos。

除了建立新的解析状态和位置之外,如果设置了该函数,则对该函数的调用将撤销ungetc并清除文件结束状态的影响。

如果发生读取或写入错误,ferror则设置流的错误指示符()。

参数

流-文件流进行修改岗位-指向fpos_t对象的指针,用作文件位置指示符的新值

返回值

0 一旦成功,否则非零值。

注意

在宽流中寻找非终点位置之后,对任何输出函数的下一次调用可能会使文件的其余部分不确定,例如通过输出不同长度的多字节序列。

带有错误检查的fsetpos。

#include #include 

int main(void){    /* Prepare an array of f-p values. */

#define SIZE 5

double A[SIZE] = {1.,2.,3.,4.,5.};    /* Write array to a file. */

FILE * fp = fopen("test.bin", "wb");    fwrite(A,sizeof(double),SIZE,fp);    fclose (fp);

/* Read the f-p values into array B. */

double B[SIZE];

fp = fopen("test.bin","rb");

fpos_t pos;    if (fgetpos(fp,&pos) != 0)      /* current position: start of file */    {       perror("fgetpos()");       fprintf(stderr,"fgetpos() failed in file %s at line # %d\n", __FILE__,__LINE__-3);       exit(EXIT_FAILURE);    }

int ret_code = fread(B,sizeof(double),1,fp);   /* read one f-p value */    /* current position: after reading one f-p value */    printf("%.1f\n", B[0]);   /* print one f-p value */

if (fsetpos(fp,&pos) != 0)   /* reset current position to start of file */    {       if (ferror(fp))       {          perror("fsetpos()");          fprintf(stderr,"fsetpos() failed in file %s at line # %d\n", __FILE__,__LINE__-5);          exit(EXIT_FAILURE);       }    }

ret_code = fread(B,sizeof(double),1,fp);   /* reread first f-p value */    printf("%.1f\n", B[0]);                    /* print one f-p value    */    fclose(fp);

return EXIT_SUCCESS; }

输出:

1.01.0

参考

C11标准(ISO / IEC 9899:2011):7.21.9.3 fsetpos函数(p:337)

C99标准(ISO / IEC 9899:1999):7.19.9.3 fsetpos函数(p:303)

C89 / C90标准(ISO / IEC 9899:1990):4.9.9.3 fsetpos函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值