Random Access file in C
0 716
Introduction to Random Access Files in C
Random access files in C empower us with the ability to read from or write to any part of a disk file without the need to sequentially process all preceding data.
This feature offers unparalleled flexibility in searching, editing, or deleting data within a file.
Although random access files share the same file opening mechanism as sequential files in C, they necessitate a distinct set of functions for manipulation, offering enhanced flexibility, power, and disk access speed.
Types of Access Files:
Sequential Access:
Sequential access involves reading data sequentially from the start of a file.
While suitable for processing data in a linear manner, it's inefficient for accessing arbitrary positions in large files.
Random Access:
Random access allows direct reading or writing of data at any position within a file, without the need to process preceding data.
This capability facilitates efficient data retrieval, modification, or deletion.
Functions for Random Access File:
Three primary functions facilitate random access file operations in C:
ftell(): Determines the current position of the file pointer relative to the file's beginning.
rewind(): Resets the file pointer to the beginning of the file.
fseek(): Moves the file pointer to a specified location within the file.
How to Use C's ftell() Function
The ftell() function retrieves the current position of the file pointer relative to the file's beginning.
syntax:
pos = ftell(FILE *fp);
For example, if ftell() returns 5 for a file with 20 bytes of data, 5 bytes have been read or written.
How to Use C's rewind() Function
The rewind() function resets the file pointer to the beginning of the file, facilitating operations like updating files.
How to Use C's fseek() Function
syntax:
int fseek(FILE *fp, long displacement, int origin);
It allows various operations such as moving to the beginning or end of the file, advancing or rewinding positions, and more.
Creating a Random Access File in C:
Random access files can be created using functions like fopen(), enabling writing operations at specific file locations.
For instance, fseek() can be combined with fwrite() to save data at designated positions within the file.
This comprehensive understanding of random access files in C empowers efficient data handling, enhancing program flexibility and performance.
Example:
// program for Random Access File in C #include<stdio.h>struct Record { int id; char name[20]; }; int main() { FILE *fp; struct Record record; // Open file in binary mode for reading and writing fp = fopen("records.bin", "rb+"); // Write record at specific position fseek(fp, 2 * sizeof(struct Record), SEEK_SET); record.id = 3; sprintf(record.name, "John"); fwrite(&record, sizeof(struct Record), 1, fp); // Read record from specific position fseek(fp, 1 * sizeof(struct Record), SEEK_SET); fread(&record, sizeof(struct Record), 1, fp); printf("Record ID: %d\n", record.id); printf("Record Name: %s\n", record.name); // Close file fclose(fp); return 0; }
Output:
Segmentation fault
Share:
Comments
Waiting for your comments