Introduction
Random access (or direct access) in a file refers to the ability to move the file pointer to any position in the file, allowing reading from or writing to a specific part of the file without having to sequentially read through the file. This is useful when you want to modify or retrieve data from any arbitrary location in a file rather than reading or writing in order from the start.
We can move forward or backward in a file using standard functions to simulate random access.
Functions for moving around in a file are.
fseek()
fseek() function in 'C' can be used to move forward or backward in a file.
Syntax of fseek()
fseek(<file poniter>, <offset>, <position>;
Where "file Pointer" points to a file that has been opened. The file can be opened in either writing mode or reading mode. fseek() can be used for moving in the file irrespective of the mode in which the file has been opened. "Offset" is the number of bytes we want to move from the current position in the file, which is indicated by the file pointer. And "Position" indicates the location in the file from where one wishes to move.
"Position" can take only three values.
- 0 meaning movement from the beginning of the file.
- 1 meaning movement from the current position in the file.
- 2 meaning movement from the end of the file.
The "offset" can be positive, meaning movement forward in the file, or negative, meaning movement backward in the file.
The following table illustrates different ways in which the F seek function can be used.
ftell()
ftell() function is used to return the current file position in a file. This function returns a long value, using which we can find the number of bytes that have been written or read from the file.
Syntax of ftell()
<long variable> = ftell(<file pointer>);
Where "long variables" will hold the relative position, i.e., it will give the offset(in bytes) of the current position.
Write a source code in C to illustrate the use of fseek() and ftell() functions.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
FILE *file_pointer;
long int firstno;
clrscr();
printf("This Shikha Tiwari's program illustrates the fseek() and ftell() function in C\n\n");
if((file_pointer = fopen("accounts.dat", "r")) == NULL)
{
printf("Cannot open account.dat file, terminating .......\n");
exit(1);
}
printf("1. The current offset in the file is %ld \n", ftell(file_pointer));
fseek(file_pointer, 10, SEEK_SET);
printf("2. The current offset in the file is %ld \n", ftell(file_pointer));
fseek(file_pointer, 10, SEEK_CUR);
printf("3. The current offset in the file is %ld \n", ftell(file_pointer));
fseek(file_pointer, -10, SEEK_END);
printf("4. The current offset in the file is %ld \n", ftell(file_pointer));
fseek(file_pointer, 10, SEEK_SET);
printf("5. The current offset in the file is %ld \n", ftell(file_pointer));
fseek(file_pointer, 0L, SEEK_SET);
printf("6. The current offset in the file is %ld \n", ftell(file_pointer));
fseek(file_pointer, 0L, SEEK_END);
printf("7. The current offset in the file is %ld \n", ftell(file_pointer));
fclose(file_pointer);
getch();
}
Output
rewind()
rewind() function is used to move to the first byte in the file, i.e., move to the beginning of the file from anywhere in the file.
Syntax of rewind()
rewind(<file pointer>);
Where "file pointer" is used to access the file.
Source code to illustrate the use of the rewind() function.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file_pointer;
char text_to_read_write[80];
long int firstno;
clrscr();
printf("This shikha Tiwari's program illustrate the use of rewind() function in C \n\n");
if((file_pointer = fopen("accounts.dat" , "r")) == NULL)
{
printf("cannot open Accounts.dat` file, terminating .......\n");
exit(1);
}
printf("the following is the text in the file :\n");
while(!feof(file_pointer))
{
fgets(text_to_read_write, 80 , file_pointer);
puts(text_to_read_write);
}
printf("the current offset in the file is %1d \n", ftell(file_pointer));
ftell(file_pointer);
printf("at this stage, the end of file has been reached , now using rewind() \n");
rewind(file_pointer);
printf("the following is the text in the file after using rewind(): \n");
while(!feof(file_pointer))
{
fgets(text_to_read_write, 80 , file_pointer);
puts(text_to_read_write);
}
printf("the current offset in the file is %1d \n", ftell(file_pointer));
fclose(file_pointer);
getch();
}
Output
Summary
Random access in a file refers to the ability to move the file pointer to any position, allowing for direct reading or writing at specific locations without the need to process the file sequentially. This is especially useful for modifying or retrieving data from arbitrary parts of the file. Functions like fseek() help move the pointer to a specified position, ftell() retrieves the current position of the pointer, and rewind() resets the pointer to the start of the file. These functions enable efficient manipulation of file data.