名前空間
変種
操作

ftell

From cppreference.com
< c‎ | io
 
 
ファイル入出力
型とオブジェクト
        
関数
ファイルアクセス
(C95)
非書式化入出力
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)

書式付き入力
直接入出力
書式付き出力
ファイルポジショニング
ftell
エラーハンドリング
ファイル操作
 
ヘッダー <stdio.h> で定義
long ftell( FILE* stream );

ファイルストリーム stream のファイル位置指示子を返します。

ストリームがバイナリモードで開かれている場合、この関数で取得される値はファイル先頭からのバイト数です。

ストリームがテキストモードで開かれている場合、この関数が返す値は未指定であり、fseek() への入力としてのみ意味があります。

目次

[編集] パラメータ

stream - 調べるファイルストリーム

[編集] 返り値

成功した場合はファイル位置指示子、失敗した場合は -1L

エラーが発生した場合、errno 変数には実装定義の正の値が設定されます。

[編集] 注記

Windows では、2 GiB より大きいファイルを扱うために _ftelli64 を使用できます。

[編集]

エラーチェックを伴う ftell() のデモンストレーション。ファイルにいくつかの浮動小数点 (FP) 値を書き込み、その後読み取ります。

#include <stdio.h>
#include <stdlib.h>
 
/* If the condition is not met then exit the program with error message. */
void check(_Bool condition, const char* func, int line)
{
    if (condition)
        return;
    perror(func);
    fprintf(stderr, "%s failed in file %s at line # %d\n", func, __FILE__, line - 1);
    exit(EXIT_FAILURE);
}
 
int main(void)
{
    /* Prepare an array of FP values. */
    #define SIZE 5
    double A[SIZE] = {1.1, 2.0, 3.0, 4.0, 5.0};
 
    /* Write array to a file. */
    const char* fname = "/tmp/test.bin";
    FILE* file = fopen(fname, "wb");
    check(file != NULL, "fopen()", __LINE__);
 
    const int write_count = fwrite(A, sizeof(double), SIZE, file);
    check(write_count == SIZE, "fwrite()", __LINE__);
 
    fclose(file);
 
    /* Read the FP values into array B. */
    double B[SIZE];
    file = fopen(fname, "rb");
    check(file != NULL, "fopen()", __LINE__);
 
    long int pos = ftell(file); /* position indicator at start of file */
    check(pos != -1L, "ftell()", __LINE__);
    printf("pos: %ld\n", pos);
 
    const int read_count = fread(B, sizeof(double), 1, file); /* read one FP value */
    check(read_count == 1, "fread()", __LINE__);
 
    pos = ftell(file); /* position indicator after reading one FP value */
    check(pos != -1L, "ftell()", __LINE__);
    printf("pos: %ld\n", pos);
    printf("B[0]: %.1f\n", B[0]); /* print one FP value */
 
    return EXIT_SUCCESS;
}

実行結果の例

pos: 0
pos: 8
B[0]: 1.1

[編集] 参照

  • C23標準 (ISO/IEC 9899:2024)
  • 7.21.9.4 The ftell function (p: TBD)
  • C17標準 (ISO/IEC 9899:2018)
  • 7.21.9.4 The ftell function (p: TBD)
  • C11標準 (ISO/IEC 9899:2011)
  • 7.21.9.4 The ftell function (p: 337-338)
  • C99標準 (ISO/IEC 9899:1999)
  • 7.19.9.4 The ftell function (p: 303-304)
  • C89/C90標準 (ISO/IEC 9899:1990)
  • 4.9.9.4 The ftell function

[編集] 関連項目

ファイル位置指示子を取得する
(関数) [編集]
ファイル位置指示子をファイル内の特定の場所に移動する
(関数) [編集]
ファイル位置指示子をファイル内の特定の場所に移動する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)