C Program to Illustrate the use of fsetpos()

C




// C program to illustrate the use of fsetpos()
#include <stdio.h>
 
int main()
{
    // File pointer
    FILE* fp;
    // Variable to store file position
    fpos_t position;
 
    // Open file in write and read mode
    fp = fopen("myfile.txt", "w+");
 
    // Get current file position and store it
    fgetpos(fp, &position);
 
    // Write "HelloWorld!" to the file
    fputs("HelloWorld!", fp);
 
    // Set file position back to the stored position
    fsetpos(fp, &position);
 
    // Write "w3wiki" to the file from the restored
    // position
    fputs("w3wiki", fp);
 
    // setting file pointer position back to the stored
    // position
    fsetpos(fp, &position);
 
    // storing data stored in file
    char arr[50];
    fgets(arr, 14, fp);
 
    // printing output
    puts(arr);
 
    // Close the file
    fclose(fp);
 
    return (0);
}


Output

w3wiki







fsetpos() (Set File Position) in C

The fsetpos() function moves the file position indicator of the given file stream to the specified position. When fsetpos() is executed, the end-of-file indicator is reset. fsetpos() function is a part of <cstdio> header file in C Standard Library.

Similar Reads

Syntax of fsetpos()

int fsetpos(FILE *stream, const fpos_t *pos)...

C Program to Illustrate the use of fsetpos()

C // C program to illustrate the use of fsetpos() #include   int main() {     // File pointer     FILE* fp;     // Variable to store file position     fpos_t position;       // Open file in write and read mode     fp = fopen("myfile.txt", "w+");       // Get current file position and store it     fgetpos(fp, &position);       // Write "HelloWorld!" to the file     fputs("HelloWorld!", fp);       // Set file position back to the stored position     fsetpos(fp, &position);       // Write "geeksforgeeks" to the file from the restored     // position     fputs("geeksforgeeks", fp);       // setting file pointer position back to the stored     // position     fsetpos(fp, &position);       // storing data stored in file     char arr[50];     fgets(arr, 14, fp);       // printing output     puts(arr);       // Close the file     fclose(fp);       return (0); }...