
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <float.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdarg.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <time.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C library function - fgetpos()
Description
The C library function int fgetpos(FILE *stream, fpos_t *pos) gets the current file position of the stream and writes it to pos.
Declaration
Following is the declaration for fgetpos() function.
int fgetpos(FILE *stream, fpos_t *pos)
Parameters
stream − This is the pointer to a FILE object that identifies the stream.
pos − This is the pointer to a fpos_t object.
Return Value
This function returns zero on success, else non-zero value in case of an error.
Example
The following example shows the usage of fgetpos() function.
#include <stdio.h> int main () { FILE *fp; fpos_t position; fp = fopen("file.txt","w+"); fgetpos(fp, &position); fputs("Hello, World!", fp); fsetpos(fp, &position); fputs("This is going to override previous content", fp); fclose(fp); return(0); }
Let us compile and run the above program to create a file file.txt which will have the following content. First of all we get the initial position of the file using fgetpos() function and then we write Hello, World!in the file, but later we have used fsetpos() function to reset the write pointer at the beginning of the file and then over-write the file with the following content −
This is going to override previous content
Now let us see the content of the above file using the following program −
#include <stdio.h> int main () { FILE *fp; int c; int n = 0; fp = fopen("file.txt","r"); while(1) { c = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", c); } fclose(fp); return(0); }
Let us compile and run above program to produce the following result −
This is going to override previous content