Header file:
stdio.h
Synopsis:
int fgetpos(FILE *stream, fpos_t *ptr);
Description:
fgetpos gets the current position of file pointer(stream) in *ptr. Returns 0 on success, non-zero on failure.
fgetpos function C example:
#include<stdio.h>
#include<string.h>
#include<errno.h>
int main() {
FILE *fp;
char *str;
fpos_t ptr;
int val;
fp = fopen("mastering-c.txt", "w");
if (fp == NULL) {
str = strerror(errno);
perror(str);
return;
}
fgetpos(fp, &ptr); // gets current position of file pointer
fputs("Example for fgetpos...... this is the 1st line\n", fp);fputs("Example for fgetpos\n", fp);
fputs("Example for fgetpos\n", fp
printf("Do you want write at the beginning of the the file(0/1):");
scanf("%d", &val);
if(val == 1) {
fsetpos(fp, &ptr); // sets current position of file pointer
fputs("Writing at the beginning of the file\n", fp);
} else if (val == 0) {
fputs("Writing at the end of the file\n", fp);
}
fclose(fp);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Do you want write at the beginnig of the the file(0/1):0
jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
Example for fgetpos...... this is the 1st line
Example for fgetpos
Example for fgetpos
Writing at the end of the file
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Do you want write at the beginnig of the the file(0/1):1
jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
Writing at the beginning of the file
1st line
Example for fgetpos
Example for fgetpos
Do you want write at the beginnig of the the file(0/1):0
jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
Example for fgetpos...... this is the 1st line
Example for fgetpos
Example for fgetpos
Writing at the end of the file
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Do you want write at the beginnig of the the file(0/1):1
jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
Writing at the beginning of the file
1st line
Example for fgetpos
Example for fgetpos
No comments:
Post a Comment