std::fsetpos(3)
β¨ std::fsetpos(3) β¨
okay so bestie, this page is from the C++ Standard Libary man page, and we're about to make file positioning make sense. no cap, it's giving low-level C++ but make it cute. π
πΈ NAME πΈ
std::fsetpos β literally std::fsetpos, the function that sets the file position indicator and multibyte parsing state for a C file stream. she's iconic for putting the stream exactly where pos says. π
π¦ Synopsis π¦
Defined in header <cstdio>.
int fsetpos( std::FILE* stream, const std::fpos_t* pos );
So here's the vibe: it sets the file position indicator and the multibyte parsing state (if any) for the C file stream stream according to the value pointed to by pos.
Besides establishing new parse state and position, a call to this function undoes the effects of std::ungetc and clears the end-of-file state, if it is set.
If a read or write error occurs, the error indicator (std::ferror) for the stream is set. noted, bestie. π«Ά
π Parameters π
streamβ file stream to modify.posβ pointer to afpos_tobject obtained fromstd::fgetposcalled on a stream associated with the same file.
β¨ Return value β¨
0 upon success, nonzero value otherwise. Also, sets errno on failure. periodt. π
π 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. this is the part where you go "noted π" and stay safe.
π Example π
// Run this code
#include <cstdio>
#include <cstdlib>
int main()
{
// Prepare an array of floating-point values.
const int SIZE = 5;
double A[SIZE] = {1., 2., 3., 4., 5.};
// Write array to a file.
std::FILE * fp = std::fopen("test.bin", "wb");
std::fwrite(A, sizeof(double), SIZE, fp);
std::fclose(fp);
// Read the values into array B.
double B[SIZE];
fp = std::fopen("test.bin", "rb");
std::fpos_t pos;
if (std::fgetpos(fp, &pos) != 0) // current position: start of file
{
std::perror("fgetpos()");
std::fprintf(stderr, "fgetpos() failed in file %s at line # %d\n",
__FILE__, __LINE__-3);
std::exit(EXIT_FAILURE);
}
int ret_code = std::fread(B, sizeof(double), 1, fp); // read one value
// current position: after reading one value
std::printf("%.1f; read count = %d\n", B[0], ret_code); // print one value and ret_code
if (std::fsetpos(fp, &pos) != 0) // reset current position to start of file
{
if (std::ferror(fp))
{
std::perror("fsetpos()");
std::fprintf(stderr, "fsetpos() failed in file %s at line # %d\n",
__FILE__, __LINE__-5);
std::exit(EXIT_FAILURE);
}
}
ret_code = std::fread(B, sizeof(double), 1, fp); // re-read first value
std::printf("%.1f; read count = %d\n", B[0], ret_code); // print one value and ret_code
std::fclose(fp);
return EXIT_SUCCESS;
}
π« Output π«
1.0; read count = 1
1.0; read count = 1
π See also π
fgetposβ gets the file position indicator (function).ftellβ returns the current file position indicator (function).fseekβ moves the file position indicator to a specific location in a file (function).- C documentation for
fsetposβ the C doc, iykyk.
that's the whole page, bestie π source vibe: http://cppreference.com, version/date: 2024.06.10, and the page is std::fsetpos(3). now go slay those file positions β¨
π¦ 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