YouTip LogoYouTip

C Function Fsetpos

# C Library Function - fsetpos() The `fsetpos()` function is a standard C library function defined in the `` header. It is used to set the file position indicator of a given file stream to a specified position. Unlike `fseek()`, which uses a byte offset relative to a starting point, `fsetpos()` uses a platform-independent position object of type `fpos_t`. This makes it highly suitable for handling very large files or complex file systems where a simple `long` integer offset (used by `fseek`) might not be sufficient. --- ## Syntax ```c int fsetpos(FILE *stream, const fpos_t *pos); ``` ### Parameters * **`stream`**: A pointer to a `FILE` object that identifies the open file stream. * **`pos`**: A pointer to an `fpos_t` object. This object contains the position to which the file pointer will be moved. This value should have been previously obtained by a call to `fgetpos()`. ### Return Value * **`0`**: On success, the function returns zero. * **Non-zero value**: On failure, the function returns a non-zero value and sets the global variable `errno` to a positive value, which can be interpreted using `perror()`. --- ## How It Works The `fsetpos()` function works hand-in-hand with `fgetpos()`. 1. You first use `fgetpos()` to record the current position of the file stream into an `fpos_t` variable. 2. After performing read or write operations that move the file pointer, you can pass that saved `fpos_t` variable to `fsetpos()` to restore the file pointer back to the exact saved position. --- ## Code Examples ### Example 1: Overwriting File Content Using fsetpos() The following example demonstrates how to record a file position, write some text, restore the position using `fsetpos()`, and overwrite the content. ```c #include int main() { FILE *fp; fpos_t position; // Open file for reading and writing fp = fopen("file.txt", "w+"); if (fp == NULL) { perror("Error opening file"); return -1; } // Store the initial position (beginning of the file) fgetpos(fp, &position); // Write initial content fputs("Hello, World!", fp); // Reset the file pointer back to the initial position fsetpos(fp, &position); // Overwrite the content at the initial position fputs("This will overwrite the previous content", fp); fclose(fp); return 0; } ``` ### Example 2: Verifying the File Content You can use the following program to read and print the contents of `file.txt` to verify that the content was successfully overwritten: ```c #include int main() { FILE *fp; int c; // Open the file in read-only mode fp = fopen("file.txt", "r"); if (fp == NULL) { perror("Error opening file"); return -1; } // Read and print character by character until EOF while(1) { c = fgetc(fp); if( feof(fp) ) { break; } printf("%c", c); } fclose(fp); return 0; } ``` **Output:** ```text This will overwrite the previous content ``` --- ## Important Considerations ### 1. Portability and `fpos_t` The `fpos_t` type is an implementation-defined type. It is not guaranteed to be an integer or a structure; its internal representation varies across different operating systems and compilers. Therefore, you should **never** attempt to modify the contents of an `fpos_t` variable directly (e.g., doing arithmetic like `pos++`). It should only be populated by `fgetpos()` and consumed by `fsetpos()`. ### 2. Multibyte and Wide-Character Streams For wide-character (state-dependent) streams, `fpos_t` also stores the parsing state of the stream. This makes `fsetpos()` the safest and most reliable way to restore positions in wide-character or multibyte text files, where simple byte offsets do not map directly to character positions. ### 3. Clearing End-of-File (EOF) A successful call to `fsetpos()` clears the end-of-file (EOF) indicator for the stream and undoes the effects of any prior calls to `ungetc()` on the same stream.
← C Function FtellC Function Fseek β†’