?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
在頭文件<stdio.h>中定義 | ||
---|---|---|
long ftell( FILE *stream ); |
返回文件流的文件位置指示符stream
。
如果流以二進(jìn)制模式打開,則此函數(shù)獲得的值是從文件開始處的字節(jié)數(shù)。
如果流在文本模式下打開,則此函數(shù)返回的值未指定,僅作為輸入來使用fseek()
。
流 | - | 文件流來檢查 |
---|
文件位置指示器成功或EOF
發(fā)生故障時(shí)。
出錯(cuò)時(shí),該errno
變量設(shè)置為實(shí)現(xiàn)定義的正值。
與錯(cuò)誤檢查ftell。
#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"); long int pos = ftell(fp); /* position indicator at start of file */ if (pos == -1L) { perror("ftell()"); fprintf(stderr,"ftell() failed in file %s at line # %d\n", __FILE__,__LINE__-4); exit(EXIT_FAILURE); } printf("%ld\n", pos); int ret_code = fread(B,sizeof(double),1,fp); /* read one f-p value */ pos = ftell(fp); /* position indicator after reading one f-p value */ if (pos == -1L) { perror("ftell()"); fprintf(stderr,"ftell() failed in file %s at line # %d\n", __FILE__,__LINE__-4); exit(EXIT_FAILURE); } printf("%ld\n", pos); printf("%.1f\n", B[0]); /* print one f-p value */ return EXIT_SUCCESS; }
輸出:
081.0
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.21.9.4函數(shù)(p:337-338)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.19.9.4函數(shù)(p:303-304)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
4.9.9.4 ftell函數(shù)