std::fseek(3)
π std::fseek(3) β from the C++ Standard Libary π
πΈ NAME πΈ
std::fseek - std::fseek. Yes bestie, the name is literally the name, and that's iconic.
β¨ Synopsis β¨
Defined in header <cstdio>:
int fseek( std::FILE* stream, long offset, int origin );
Sets the file position indicator for the file stream stream.
If the stream is open in binary mode, the new position is exactly offset bytes measured from:
- the beginning of the file if origin is
SEEK_SET, - the current file position if origin is
SEEK_CUR, - the end of the file if origin is
SEEK_END.
Binary streams are not required to support SEEK_END, in particular if additional null bytes are output. π
If the stream is open in text mode, the only supported values for offset are:
- zero (which works with any origin), and
- a value returned by an earlier call to
std::ftellon a stream associated with the same file (which only works with origin ofSEEK_SET).
If the stream is wide-oriented, the restrictions of both text and binary streams apply (result of std::ftell is allowed with SEEK_SET and zero offset is allowed from SEEK_SET and SEEK_CUR, but not SEEK_END).
In addition to changing the file position indicator, fseek undoes the effects of std::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. π
π Parameters π
streamβ file stream to modify.offsetβ number of characters to shift the position relative to origin.originβ position to which offset is added. It can have one of the following values:SEEK_SETSEEK_CURSEEK_END
π Return value π
0 upon success, nonzero value otherwise. Slay. β¨
π§ 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 a sparse file.
- POSIX also requires that
fseekfirst performsfflushif 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, that
fseekshould return-1on error, and seterrnoto indicate the error.
Okay but fr fr, that undefined-behaviour note is not a vibe. Treat it with respect. π
π¦ 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 end
const std::size_t filesize = std::ftell(fp);
std::vector<std::uint8_t> buffer(filesize);
std::fseek(fp, 0, SEEK_SET); // seek to start
std::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
π See also π
fsetposmoves the file position indicator to a specific location in a file (function)fgetposgets the file position indicator (function)ftellreturns the current file position indicator (function)rewindmoves the file position indicator to the beginning in a file (function)- C documentation for
fseek
Source/version/date vibe check: http://cppreference.com, 2024.06.10, page std::fseek(3). Byeeee, go seek responsibly π
β¨
π¦ 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