C++ <cstdio> - SEEK_SET, SEEK_CUR, SEEK_END
The C++ <cstdio> SEEK_SET, SEEK_CUR, SEEK_END macro constants are used to set origin, which is the position to which offset is added in the fseek() function.
Macros | Description |
---|---|
SEEK_SET | Beginning of file |
SEEK_CUR | Current position of the file pointer |
SEEK_END | End of file * |
Example:
In the example below, a file is created using fopen() function. The initial content of the file is written to the file using fputs() function. Then, by using fseek() function current file position indicator is set to 10 bytes measured from beginning of the file. After that, agian by using fputs() function, the content of the file is modified.
#include <stdio.h> int main (){ FILE *pFile = fopen("test.txt", "wb"); //writes content in the file fputs("This is a test file.", pFile); //set the current file position indicator to //10 bytes measured from beginning of the file fseek(pFile, 10, SEEK_SET); //modify the content after 10 bytes fputs("modified content.", pFile); //close the file fclose(pFile); //open the file in read mode to read //the content of the file pFile = fopen("test.txt", "r"); int c = fgetc(pFile); while (c != EOF) { putchar(c); c = fgetc(pFile); } //close the file fclose(pFile); return 0; }
After the code is successfully executed, the test.txt file will contain:
This is a modified content.
❮ C++ <cstdio> Library