ftell
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <stdio.h> で定義
|
||
long ftell( FILE *stream ); |
||
ファイルストリーム stream のファイル位置指示子を返します。
ストリームがバイナリモードで開かれている場合、この関数によって取得される値はファイルの先頭からのバイト数です。
ストリームがテキストモードで開かれている場合、この関数によって返される値は未規定であり、 fseek() への入力としてのみ意味を持ちます。
引数
| stream | - | 調べるファイルストリーム |
戻り値
成功した場合はファイル位置指示子、失敗した場合は -1L。
失敗した場合は、 errno 変数が処理系定義の正の値に設定されます。
例
ftell をエラーチェック付きで使用します。
Run this code
#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;
}
出力:
0
8
1.0
参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.21.9.4 The ftell function (p: 337-338)