> ## Documentation Index
> Fetch the complete documentation index at: https://learn.acadlink.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Sequential vs Random Access Files

> Difference between sequential and random access file operations in C

# 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

```c theme={null}
#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

```c theme={null}
fseek(file_pointer, offset, position);
```

| Position Constant | Meaning               |
| ----------------- | --------------------- |
| `SEEK_SET`        | Beginning of the file |
| `SEEK_CUR`        | Current position      |
| `SEEK_END`        | End of the file       |

### Example: Random Access with Student Records

```c theme={null}
#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 Access       | Random Access                    |
| -------------------- | ----------------------- | -------------------------------- |
| Access method        | One by one, in order    | Jump directly to a position      |
| Speed for one record | Slow — scans from start | Fast — direct seek               |
| Functions            | `fgets()`, `fscanf()`   | `fseek()`, `fread()`, `fwrite()` |
| File type            | Text files              | Usually binary                   |
| Use case             | Logs, reports           | Databases, indexed records       |
