Random Access to Files
In C language, file handling is a key aspect that allows us to create, read, write, and update files. In this article, we will learn about random access to files, which is one of the most powerful features of C file handling.
What is Random Access to Files?
Random access means you can move directly to any part of a file without passing through the previous data. In contrast, sequential access requires you to pass through all previous data before reaching the desired data. Random access is beneficial when dealing with large files and you need to access specific data quickly.
File Pointers
To perform random access, we need to understand file pointers. A file pointer is a pointer that points to the current position in the file. When you open a file, the file pointer is set at the beginning of the file. When you read or write data, the file pointer moves forward.
The fseek Function
The fseek
function is used to move the file pointer to a specific location in a file. Here is its syntax:
int fseek(FILE *pointer, long int offset, int position)
The function returns zero if it successfully moves the file pointer, and a non-zero value if it fails. The parameters used in the function are:
pointer
: This is a pointer to the file.offset
: This is the number of bytes from the position that you want to move.position
: This is the position from where you want to move the file pointer. It can have three values:SEEK_SET
: Beginning of the fileSEEK_CUR
: Current positionSEEK_END
: End of the file
Here is an example:
#include<stdio.h>
int main() {
FILE *fp;
fp = fopen("file.txt", "w+");
fputs("This is a random access test", fp);
fseek(fp, 7, SEEK_SET);
fputs(" C programming Language", fp);
fclose(fp);
return(0);
}
In this code, we first open file.txt
in w+
mode. Then, we write "This is a random access test" into the file. After that, we use fseek
to move the file pointer 7 positions forward from the beginning of the file, and then we write " C programming Language". The final content of the file will be "This is C programming Language".
The ftell Function
The ftell
function is used to find the current position of the file pointer. Here is its syntax:
long int ftell(FILE *pointer)
The function returns the current position of the file pointer from the beginning of the file. Here is an example:
#include<stdio.h>
int main() {
FILE *fp;
int len;
fp = fopen("file.txt", "r");
if(fp == NULL) {
perror("Error opening file");
return(-1);
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fclose(fp);
printf("Total size of file.txt = %d bytes", len);
return(0);
}
In this code, we first open file.txt
in r
mode. Then, we move the file pointer to the end of the file using fseek
. After that, we use ftell
to get the current position of the file pointer, which is the total size of the file. Finally, we print the size of the file.
The rewind Function
The rewind
function is used to move the file pointer to the beginning of the file. Here is its syntax:
void rewind(FILE *pointer)
Here is an example:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("file.txt", "r");
if(fp == NULL) {
perror("Error opening file");
return(-1);
}
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
rewind(fp);
printf("\nAfter rewind the file content is:\n");
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return(0);
}
In this code, we first open file.txt
in r
mode. Then, we read the content of the file and print it. After that, we use rewind
to move the file pointer to the beginning of the file, and then we read and print the content of the file again.
In conclusion, random access to files in C allows you to directly access any part of a file without having to pass through all the previous data, which can be a powerful tool when dealing with large files.