Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ftell

      From cppreference.com
      <cpp‎ |io‎ |c
       
       
       
       
      Defined in header<cstdio>
      long ftell(std::FILE* stream);

      Returns the current value of the file position indicator for the file streamstream.

      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 tostd::fseek.

      Contents

      [edit]Parameters

      stream - file stream to examine

      [edit]Return value

      File position indicator on success or-1L if failure occurs. Also setserrno on failure.

      [edit]Notes

      On Windows,_ftelli64 can be used to work with files larger than 2 GiB.

      [edit]Example

      Demonstratesstd::ftell() with error checking. Writes then reads a few floating-point (FP) values to/from a file.

      Run this code
      #include <cstdio>#include <cstdlib>#include <iostream> // If the condition is not met then exit the program with error message.void check(bool condition,constchar* 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.constexprint SIZE{5};double A[SIZE]={1.1,2.2,3.3,4.4,5.5}; // Write array to a file.constchar* fname="/tmp/test.bin";    FILE* file=std::fopen(fname,"wb");    check(file!=NULL,"fopen()", __LINE__); constint 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'; constint 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 returnEXIT_SUCCESS;}

      Possible output:

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

      [edit]See also

      gets the file position indicator
      (function)[edit]
      moves the file position indicator to a specific location in a file
      (function)[edit]
      moves the file position indicator to a specific location in a file
      (function)[edit]
      returns the input position indicator
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      returns the output position indicator
      (public member function ofstd::basic_ostream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/c/ftell&oldid=178172"

      [8]ページ先頭

      ©2009-2025 Movatter.jp