Namespaces
Variants

ftell

From cppreference.net
< c ‎ | io
Definiert in Header <stdio.h>
long ftell ( FILE * stream ) ;

Gibt die Dateipositionsanzeige für den Dateistrom stream zurück.

Wenn der Stream im Binärmodus geöffnet ist, ist der von dieser Funktion erhaltene Wert die Anzahl der Bytes vom Anfang der Datei.

Wenn der Stream im Textmodus geöffnet ist, ist der von dieser Funktion zurückgegebene Wert unspezifiziert und nur als Eingabe für fseek() sinnvoll.

Inhaltsverzeichnis

Parameter

stream - zu untersuchender Dateistrom

Rückgabewert

Dateipositionsanzeiger bei Erfolg oder - 1L bei Auftreten eines Fehlers.

Bei einem Fehler wird die Variable errno auf einen implementierungsdefinierten positiven Wert gesetzt.

Hinweise

Unter Windows kann _ftelli64 verwendet werden, um mit Dateien größer als 2 GiB zu arbeiten.

Beispiel

Demonstriert die Verwendung von ftell() mit Fehlerüberprüfung. Schreibt und liest einige Gleitkommawerte (FP-Werte) von/zu einer Datei.

#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;
}

Mögliche Ausgabe:

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

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.21.9.4 Die ftell-Funktion (S.: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.21.9.4 Die ftell-Funktion (S.: TBD)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.21.9.4 Die ftell-Funktion (S: 337-338)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.19.9.4 Die ftell-Funktion (S. 303-304)
  • C89/C90 Standard (ISO/IEC 9899:1990):
  • 4.9.9.4 Die ftell-Funktion

Siehe auch

ruft den Dateipositionszeiger ab
(Funktion)
bewegt den Dateipositionszeiger an eine bestimmte Position in einer Datei
(Funktion)
bewegt den Dateipositionszeiger an eine bestimmte Position in einer Datei
(Funktion)