C Random File Access

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Random File Access?

Every stream has a position indicator. fseek(fp, offset, origin) moves it (SEEK_SET, SEEK_CUR, SEEK_END), ftell(fp) reports it, and rewind(fp) returns to the start. Random access is reliable for binary files; for text files the offsets are only meaningful when returned by ftell.

Remember
fseek(fp, 2 * sizeof(int), SEEK_SET);  /* to 3rd int */
long pos = ftell(fp);                    /* current offset */
fseek(fp, 0, SEEK_END);                  /* to the end     */
rewind(fp);                              /* back to start  */

Cheat Sheet

Keep these facts handy while you work with Random File Access in C.

TopicDetail
SEEK_SETFrom the start
SEEK_CURFrom the current position
SEEK_ENDFrom the end
ftellReturns current offset or -1

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: File Error Handling.

Example 1 — Read the third record directly

Skip two integers without reading them.

C
#include <stdio.h>

int main(void) {
    int vals[] = {10, 20, 30, 40, 50};
    FILE *fp = fopen("seek.bin", "wb");
    if (!fp) return 1;
    fwrite(vals, sizeof vals[0], 5, fp);
    fclose(fp);

    fp = fopen("seek.bin", "rb");
    if (!fp) return 1;

    int x;
    fseek(fp, 2 * (long)sizeof(int), SEEK_SET);
    fread(&x, sizeof x, 1, fp);
    printf("third value: %d\n", x);

    fseek(fp, 0, SEEK_END);
    printf("items in file: %ld\n", ftell(fp) / (long)sizeof(int));
    fclose(fp);
    return 0;
}

How It Works

  1. fseek jumps 2 ints (8 bytes) from the start.
  2. After the read the position moves to the next item.
  3. SEEK_END plus ftell gives the file size in bytes; dividing by sizeof(int) gives the item count.

Edge Cases & Pitfalls

Check these before relying on Random File Access in real programs.

text

Seeking in text files

Text offsets may not equal byte counts on some platforms. Save and restore only values from ftell.

mix

Read/write switch

After a write, call fseek or fflush before reading in + modes.

Key Takeaways

  • Jump: fseek by byte offset.
  • Tell: ftell current position.
  • Binary: best for random access.

One line: Every stream has a position indicator.

Frequently Asked Questions

Open it in binary mode, call fseek(fp, 0, SEEK_END), and read ftell(fp). Then rewind or seek back.
rewind(fp) sets the position to the start and clears the error and end-of-file indicators.
Continue with "C File Error Handling" next. Detect and report failures with errno, perror, ferror, and feof. The C File Handling hub lists every topic in order.

Next: File Error Handling

Detect and report failures with errno, perror, ferror, and feof.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful