Functions like fseek(), ftell() and rewind() are included in the “Random Access to file” topic. Those functions we are going to discuss in this blog.
fprintf()
This function is used to write multiple variables of multiple data types into a file. Suppose if we want to write an integer array and one float variable into a file then we can use this function.
fprintf(<file pointer name>,”<control string for var 1, control string for var 2,… control string for var n>”,<var 1 name, var 2 name,… var n name>);
Suppose we want to make file pointer fp1 to point to a text file having the name f1.txt. and we want to write “ABCD” and 10 into file f1.txt. So for ease, we have stored “ABCD” in variable c(character array) and 10 in the variable i (integer variable). Then below is an example of fprintf syntax. Control string related to “ABCD” is %s and related to 10 is %d.
int i=10; char c[10]=”ABCD”; fprintf(fp1,“%s %d”,c,i);
ftell()
This function returns the position of the file pointer. To hold its return value, we need an int variable.
ftell(<file pointer name >);
To understand it, let’s take an example of a file pointer having the name fp1. It points to a text file having the name f1.txt. And i1 is the int variable. The value returned from the ftell function is stored in the i1 variable.
FILE *f1; //code int i1; i1 = ftell(fp1); fprintf(“pointer position : %d”,i1);
rewind()
This function is used to reset the position of the file pointer. After using this function, the position of the file pointer will be “0”. Below is the syntax for the same.
rewind(<file pointer name >);
To understand it, let’s take the same example above. After the below code, the position of file pointer fp1 will be 0.
FILE *f1; //code rewind(fp1);
fseek()
This function is used to change the position of the file pointer. Below is the syntax.
fseek(<file pointer name>,<offset>,<position>);
Offset: Number of steps we want to move. |
If offset is n then the pointer will move n steps forward. (means if n is 3 then the pointer will move 3 steps forward)
If offset is -n then the pointer will move n steps backward. (means if n is -3 then the pointer will move 3 steps backward) |
Position: From where we want to apply offset, means from where we want to move backward or forward using offset. |
If position value is,
SEEK_SET or 0: Apply offset changes from starting of the file(0) SEEK_CUR or 1: Apply offset changes from the current position of the file pointer SEEK_END or 2: Apply offset changes from the end of the file |
Let’s take the same previous example. We want to now move 3 steps forward (so the offset will be 3) from the start of the file (so the position will be 0).
FILE *f1; //code fseek(fp1,3,0);
Example program structure to understand the use of random access FILE functions
In the below example, f1 is a file name and fp1 is a file pointer. First, we have used the rewind function and we are storing the value of file pointer position in int variable a. Now we are printing the value of a (file pointer fp1 position) which is 0 because we have used the rewind function. After that we are using the fseek function to move 3 steps forward (offset = 3) from the starting of the file(position = 0).
Now we are printing the value of the file pointer position using the ftell function.
#include<stdio.h> #include<conio.h> void main() { int a; FILE *fp1; clrscr(); fp1=fopen("F:/content/c/f1.txt","r"); rewind(fp1); a=ftell(fp1); printf("file position 1 : %d \n",a); fseek(fp1,3,0); a=ftell(fp1); printf("file position 2 : %d ",a); getch(); }
Question:
- Which functions show the working of “Random access to FILE”.
- The syntax for rewind () and ftell() functions.
- Need of ftell() and rewind() functions.
- The syntax and need of the fseek() function.
- What is the meaning of offset in fseek() function?