?
This document uses PHP Chinese website manual Release
在頭文件<stdio.h>中定義 | ||
---|---|---|
int fsetpos(FILE * stream,const fpos_t * pos); |
stream
根據(jù)指向的值設(shè)置文件流的文件位置指示符和多字節(jié)解析狀態(tài)(如果有)pos
。
除了建立新的解析狀態(tài)和位置之外,如果設(shè)置了該函數(shù),則對(duì)該函數(shù)的調(diào)用將撤銷(xiāo)ungetc
并清除文件結(jié)束狀態(tài)的影響。
如果發(fā)生讀取或?qū)懭脲e(cuò)誤,ferror
則設(shè)置流的錯(cuò)誤指示符()。
流 | - | 文件流進(jìn)行修改 |
---|---|---|
崗位 | - | 指向fpos_t對(duì)象的指針,用作文件位置指示符的新值 |
0
一旦成功,否則非零值。
在寬流中尋找非終點(diǎn)位置之后,對(duì)任何輸出函數(shù)的下一次調(diào)用可能會(huì)使文件的其余部分不確定,例如通過(guò)輸出不同長(zhǎng)度的多字節(jié)序列。
帶有錯(cuò)誤檢查的fsetpos。
#include <stdio.h>#include <stdlib.h> 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標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.21.9.3 fsetpos函數(shù)(p:337)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.19.9.3 fsetpos函數(shù)(p:303)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
4.9.9.3 fsetpos函數(shù)