std::ftell(3)
π std::ftell(3) β your fave from the C++ Standard Libary β¨
π NAME
std::ftell β std::ftell, thatβs literally the whole name, periodt π
β¨ Synopsis β¨
Defined in header <cstdio>:
long ftell( std::FILE* stream );
Returns the current value of the file position indicator for the file stream stream π
If the stream is open in binary mode, the value obtained by this function is the number of bytes from the beginning of the file πΈ
If the stream is open in text mode, the value returned by this function is unspecified and is only meaningful as the input to std::fseek β οΈ no cap, thatβs the rule.
π Parameters π
streamβ file stream to examine π
π«Ά Return value π«Ά
File position indicator on success, or -1L if failure occurs. Also sets errno on failure π
πΈ Example πΈ
Demonstrates std::ftell() with error checking. Writes then reads a few floating-point (FP) values to/from a file π¦
// Run this code β like, actually run it, bestie:
#include <cstdio>
#include <cstdlib>
#include <iostream>
// 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;
std::perror(func);
std::cerr << func << " failed in file " << __FILE__ << " at line # " << line - 1
<< '\n';
std::exit(EXIT_FAILURE);
}
int main()
{
// Prepare an array of FP values.
constexpr int SIZE {5};
double A[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};
// Write array to a file.
const char* fname = "/tmp/test.bin";
FILE* file = std::fopen(fname, "wb");
check(file != NULL, "fopen()", __LINE__);
const int write_count = std::fwrite(A, sizeof(double), SIZE, file);
check(write_count == SIZE, "fwrite()", __LINE__);
std::fclose(file);
// Read the FP values into array B.
double B[SIZE];
file = std::fopen(fname, "rb");
check(file != NULL, "fopen()", __LINE__);
long pos = std::ftell(file); // position indicator at start of file
check(pos != -1L, "ftell()", __LINE__);
std::cout << "pos: " << pos << '\n';
const int read_count = std::fread(B, sizeof(double), 1, file); // read one FP value
check(read_count == 1, "fread()", __LINE__);
pos = std::ftell(file); // position indicator after reading one FP value
check(pos != -1L, "ftell()", __LINE__);
std::cout << "pos: " << pos << '\n';
std::cout << "B[0]: " << B[0] << '\n'; // print one FP value
return EXIT_SUCCESS;
}
π¦ Possible output π¦
pos: 0
pos: 8
B[0]: 1.1
π See also π
fgetposβ gets the file position indicator (function) πfseekβ moves the file position indicator to a specific location in a file (function) πfsetposβ moves the file position indicator to a specific location in a file (function) π―tellgβ returns the input position indicator (public member function ofstd::basic_istream<CharT,Traits>) π₯tellpβ returns the output position indicator (public member function ofstd::basic_ostream<CharT,Traits>) π€- C documentation for
ftellβ the C docs, literally π
π source/version vibes: this page came from http://cppreference.com, stamped 2024.06.10, and itβs the std::ftell(3) manual page. byeee π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms