C fgetpos() function
C library function - fgetpos()
The fgetpos() function is used to store the current position of the file pointer that is associated with stream into the object pointed to by pos. The value pointed to by pos can be used later in a call to fsetpos() to reposition the stream.
Syntax:
int fgetpos(FILE *stream, fpos_t *pos)
fgetpos() Parameters:
Name | Description | Required /Optional |
---|---|---|
stream | Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. | Required |
pos | This is the pointer to a fpos_t object. | Required |
Return value from fgetpos()
- Upon successful completion, fgetpos() shall return 0; otherwise, it shall return a non-zero value and set errno to indicate the error.
Example: fgetpos() function
Following example opens the file test.txt for reading and stores the current file pointer position into the variable position.
#include <stdio.h>
#include <string.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);
}
Output:
Current position of file pointer found!
Errors: The fgetpos() function shall fail if:-
- The file descriptor underlying stream is not valid
- The current value of the file position cannot be represented correctly in an object of type fpos_t.
- The file descriptor underlying stream is associated with a pipe, FIFO, or socket.
C Programming Code Editor:
Previous C Programming: C fflush()
Next C Programming: C fopen()
C Programming: Tips of the Day
Reading a string with scanf :
An array "decays" into a pointer to its first element, so scanf("%s", string) is equivalent to scanf("%s", &string[0]). On the other hand, scanf("%s", &string) passes a pointer-to-char[256], but it points to the same place.
Then scanf, when processing the tail of its argument list, will try to pull out a char *. That's the Right Thing when you've passed in string or &string[0], but when you've passed in &string you're depending on something that the language standard doesn't guarantee, namely that the pointers &string and &string[0] -- pointers to objects of different types and sizes that start at the same place -- are represented the same way.
Ref : https://bit.ly/3pdEk6f
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises