Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
When it comes to storing and retrieving data in a computer program, there are different approaches that can be used depending on the specific needs of the application. Two common types of files used in C language are sequential files & random access files. Sequential files allow data to be accessed in a linear fashion, one record after another, while random access files enable direct access to any record in the file.
In this article, we will discuss the differences between these two file types, understand their characteristics, and see how they are used in C programs with code examples.
What is a Sequential File?
A sequential file is a type of file where data is stored & accessed in a sequential or linear manner. Records in a sequential file are stored one after another, in the order they were written to the file. When reading from a sequential file, you must start from the beginning & read each record in sequence until you reach the desired record or the end of the file.
In this example, we open a file named "data.txt" in write mode using fopen(). We then prompt the user to enter a name & age, which are then written to the file using fprintf(). Finally, we close the file using fclose().
To read data from a sequential file, you would open the file in read mode & use functions like fscanf() or fgets() to read the records sequentially.
What is a Random Access File?
A random access file allows you to access and modify records in the file directly by their position or record number without having to read through all the preceding records. Each record in a random access file has a fixed size, and the file maintains a file pointer that indicates the current position in the file.
To create a random access file in C, you need to open the file in binary mode using the "rb+" mode.
For example:
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
FILE *file;
struct Student student;
file = fopen("students.dat", "rb+");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
// Write a student record at the beginning of the file
strcpy(student.name, "Ravi");
student.age = 20;
fwrite(&student, sizeof(struct Student), 1, file);
// Move the file pointer to the second record
fseek(file, sizeof(struct Student), SEEK_SET);
// Write another student record
strcpy(student.name, "Sinki");
student.age = 22;
fwrite(&student, sizeof(struct Student), 1, file);
fclose(file);
return 0;
}
In this example, we define a Student structure that represents a record in the file. We open the file in "rb+" mode, which allows both reading & writing in binary mode.
We write the first student record at the beginning of the file using fwrite(). Then, we use fseek() to move the file pointer to the second record position by specifying an offset of sizeof(struct Student) bytes from the beginning of the file (SEEK_SET).
Finally, we write the second student record at the current position using fwrite() & close the file.
To read a specific record from a random access file, you can use fseek() to move the file pointer to the desired record position & then use fread() to read the record.
Difference between Sequential and Random Access File
Parameters
Sequential File
Random Access File
Access Method
Records are accessed sequentially, one after another
Records can be accessed directly by their position or record number
File Pointer
File pointer moves sequentially as records are read or written
File pointer can be moved to any position in the file
Record Size
Records can have variable sizes
Records have a fixed size
Insertion and Deletion
Difficult and inefficient, requires rewriting the entire file
Easy and efficient, can be done by overwriting specific records
Searching
Requires sequential search from the beginning of the file
Allows direct access to any record based on its position
Disk Space Utilization
Efficient, as records are stored consecutively
May have unused space between records due to fixed record size
Suitable Use Cases
Suitable for batch processing and data that is accessed sequentially
Suitable for applications that require frequent random access to records
Examples
Log files, transaction records, data streams
Database files, indexing systems, lookup tables
Frequently Asked Questions
Can sequential files be accessed randomly?
No, sequential files can only be accessed sequentially from the beginning of the file.
Are random access files more efficient than sequential files?
Random access files are more efficient for applications that require frequent random access to records, while sequential files are more efficient for batch processing and sequential data access.
Can the record size vary in a random access file?
No, the record size in a random access file must be fixed to allow direct access to individual records.
Conclusion
In this article, we discussed the differences between sequential files and random access files in C . We learned that sequential files store records sequentially and are accessed linearly, while random access files allow direct access to records based on their position. We also compared the two file types based on various parameters such as access speed, record size, file pointer, insertion/deletion, and suitable use cases.