Skip to main content

Sequential vs Random Access Files in C


1. Sequential Access Files

In sequential access, data is read or written one record at a time, in order — from the beginning to the end. You cannot jump to a specific position; you must read through everything before it. Common functions: fgetc(), fgets(), fprintf(), fscanf()

Example: Menu-Driven Sequential File

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    char name[20], line[100];
    int age, choice;

    do {
        printf("\n***** SELECT CHOICE *****\n");
        printf("1. Write Data to File\n");
        printf("2. Read Data from File\n");
        printf("3. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                fp = fopen("Student.txt", "a");
                if (fp == NULL) { printf("Error\n"); return 1; }
                printf("Enter name and age: ");
                scanf("%s %d", name, &age);
                fprintf(fp, "Name: %s\nAge: %d\n", name, age);
                fclose(fp);
                printf("Written successfully!\n");
                break;

            case 2:
                fp = fopen("Student.txt", "r");
                if (fp == NULL) { printf("Error\n"); return 1; }
                printf("\n--- File Contents ---\n");
                while (fgets(line, sizeof(line), fp) != NULL)
                    printf("%s", line);
                fclose(fp);
                break;

            case 3:
                printf("Exiting...\n");
                exit(0);

            default:
                printf("Invalid choice.\n");
        }
    } while (1);

    return 0;
}

2. Random Access Files

In random access, you can jump directly to any position in the file using fseek() — no need to read through the whole file first. This is much faster for large files. Key functions: fseek(), ftell(), rewind()

fseek() Syntax

fseek(file_pointer, offset, position);
Position ConstantMeaning
SEEK_SETBeginning of the file
SEEK_CURCurrent position
SEEK_ENDEnd of the file

Example: Random Access with Student Records

#include <stdio.h>
#include <stdlib.h>

struct Student {
    int  id;
    char name[30];
    int  age;
};

int main() {
    FILE *fp;
    struct Student s;
    int choice, id, found;

    fp = fopen("students.dat", "ab+");
    if (fp == NULL) { printf("Error opening file!"); return 1; }

    while (1) {
        printf("\n--- MENU ---\n");
        printf("1. Add Record\n2. Display All\n3. Search by ID\n4. Update Record\n5. Exit\n");
        printf("Choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                fseek(fp, 0, SEEK_END);
                printf("Enter ID, Name, Age: ");
                scanf("%d %s %d", &s.id, s.name, &s.age);
                fwrite(&s, sizeof(s), 1, fp);
                printf("Record added!\n");
                break;

            case 2:
                rewind(fp);
                printf("\nAll Records:\n");
                while (fread(&s, sizeof(s), 1, fp) == 1)
                    printf("ID: %d  Name: %s  Age: %d\n", s.id, s.name, s.age);
                break;

            case 3:
                printf("Enter ID to search: ");
                scanf("%d", &id);
                rewind(fp);
                found = 0;
                while (fread(&s, sizeof(s), 1, fp) == 1) {
                    if (s.id == id) {
                        printf("Found: %s, Age: %d\n", s.name, s.age);
                        found = 1;
                        break;
                    }
                }
                if (!found) printf("Record not found.\n");
                break;

            case 4:
                printf("Enter ID to update: ");
                scanf("%d", &id);
                rewind(fp);
                found = 0;
                while (fread(&s, sizeof(s), 1, fp) == 1) {
                    if (s.id == id) {
                        printf("Enter new Name and Age: ");
                        scanf("%s %d", s.name, &s.age);
                        fseek(fp, -(long)sizeof(s), SEEK_CUR);
                        fwrite(&s, sizeof(s), 1, fp);
                        printf("Record updated!\n");
                        found = 1;
                        break;
                    }
                }
                if (!found) printf("Record not found.\n");
                break;

            case 5:
                fclose(fp);
                printf("Exiting...\n");
                exit(0);
        }
    }
    return 0;
}

Comparison

Sequential AccessRandom Access
Access methodOne by one, in orderJump directly to a position
Speed for one recordSlow — scans from startFast — direct seek
Functionsfgets(), fscanf()fseek(), fread(), fwrite()
File typeText filesUsually binary
Use caseLogs, reportsDatabases, indexed records