Table of contents
1.
Introduction
2.
What is a Sequential File?
3.
What is a Random Access File?
4.
Difference between Sequential and Random Access File 
5.
Frequently Asked Questions
5.1.
Can sequential files be accessed randomly?
5.2.
Are random access files more efficient than sequential files?
5.3.
Can the record size vary in a random access file?
6.
Conclusion
Last Updated: Dec 10, 2024
Easy

Sequential and Random Access File in C

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Sequential and Random Access File in C

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.

For example: 

#include <stdio.h>

int main() {
    FILE *file;
    char name[50];
    int age;

    file = fopen("data.txt", "w");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    printf("Enter name: ");
    scanf("%s", name);
    printf("Enter age: ");
    scanf("%d", &age);

    fprintf(file, "%s\n%d\n", name, age);

    fclose(file);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


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;
}
You can also try this code with Online C Compiler
Run Code


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 

ParametersSequential FileRandom Access File
Access MethodRecords are accessed sequentially, one after anotherRecords can be accessed directly by their position or record number
File PointerFile pointer moves sequentially as records are read or writtenFile pointer can be moved to any position in the file
Record SizeRecords can have variable sizesRecords have a fixed size
Insertion and DeletionDifficult and inefficient, requires rewriting the entire fileEasy and efficient, can be done by overwriting specific records
SearchingRequires sequential search from the beginning of the fileAllows direct access to any record based on its position
Disk Space UtilizationEfficient, as records are stored consecutivelyMay have unused space between records due to fixed record size
Suitable Use CasesSuitable for batch processing and data that is accessed sequentiallySuitable for applications that require frequent random access to records
ExamplesLog files, transaction records, data streamsDatabase 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.

You can also check out our other blogs on Code360.

Live masterclass