Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::fseek

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

      Sets the file position indicator for the file streamstream.

      If thestream is open in binary mode, the new position is exactlyoffset bytes measured from the beginning of the file iforigin isSEEK_SET, from the current file position iforigin isSEEK_CUR, and from the end of the file iforigin isSEEK_END. Binary streams are not required to supportSEEK_END, in particular if additional null bytes are output.

      If thestream is open in text mode, the only supported values foroffset are zero (which works with anyorigin) and a value returned by an earlier call tostd::ftell on a stream associated with the same file (which only works withorigin ofSEEK_SET).

      If thestream is wide-oriented, the restrictions of both text and binary streams apply (result ofstd::ftell is allowed withSEEK_SET and zero offset is allowed fromSEEK_SET andSEEK_CUR, but notSEEK_END).

      In addition to changing the file position indicator,fseek undoes the effects ofstd::ungetc and clears the end-of-file status, if applicable.

      If a read or write error occurs, the error indicator for the stream (std::ferror) is set and the file position is unaffected.

      Contents

      [edit]Parameters

      stream - file stream to modify
      offset - number of characters to shift the position relative to origin
      origin - position to whichoffset is added. It can have one of the following values:SEEK_SET,SEEK_CUR,SEEK_END

      [edit]Return value

      0 upon success, nonzero value otherwise.

      [edit]Notes

      After seeking to a non-end position in a wide stream, the next call to any output function may render the remainder of the file undefined, e.g. by outputting a multibyte sequence of a different length.

      POSIX allows seeking beyond the existing end of file. If an output is performed after this seek, any read from the gap will return zero bytes. Where supported by the filesystem, this creates asparse file.

      POSIX also requires thatfseek first performsfflush if there are any unwritten data (but whether the shift state is restored is implementation-defined). The standard C++ file streams guarantee both flushing and unshifting:std::basic_filebuf::seekoff.

      POSIX specifies, thatfseek should return-1 on error, and seterrno to indicate the error.

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

      [edit]Example

      Run this code
      #include <cassert>#include <cstdio>#include <cstdint>#include <fstream>#include <vector> int main(){std::ofstream("dummy.nfo")<<"8 bytes\n";// create the file std::FILE* fp=std::fopen("dummy.nfo","rb");assert(fp);     std::fseek(fp,0,SEEK_END);// seek to endconststd::size_t filesize=std::ftell(fp);std::vector<std::uint8_t> buffer(filesize);     std::fseek(fp,0,SEEK_SET);// seek to startstd::fread(buffer.data(), sizeof(std::uint8_t), buffer.size(), fp); std::fclose(fp);std::printf("I've read %zi bytes\n", filesize);}

      Possible output:

      I've read 8 bytes

      [edit]See also

      moves the file position indicator to a specific location in a file
      (function)[edit]
      gets the file position indicator
      (function)[edit]
      returns the current file position indicator
      (function)[edit]
      moves the file position indicator to the beginning in a file
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/c/fseek&oldid=178173"

      [8]ページ先頭

      ©2009-2025 Movatter.jp