ADVANCED DATA STRUCTURES PROGRAM • LEVEL 19 — FILES AND FILE ORGANIZATION

Store Fixed-Length Records in a Binary File

Learn how to store fixed-length records in a binary file using a clear C program.

AdvancedRecord organizationSecondary storage

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
102 Ravi 91

COMPLETE C PROGRAM

Complete C implementation

ads-fixed-length-binary-records.c
Open in compiler
#include <stdio.h>

struct Record{int id;char name[16];int mark;};

int main(void)
{
    struct Record records[]={{101,"Asha",88},{102,"Ravi",91},{103,"Neha",85}};FILE*file=fopen("records.bin","wb");if(!file)return 1;fwrite(records,sizeof records[0],3,file);fclose(file);file=fopen("records.bin","rb");struct Record value;fseek(file,(long)sizeof value,SEEK_SET);fread(&value,sizeof value,1,file);fclose(file);printf("%d %s %d\n",value.id,value.name,value.mark);return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

102 Ravi 91
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Use equal-size binary records so byte offset equals record index times record size.
  3. Display the computed result.

Use equal-size binary records so byte offset equals record index times record size.

EFFICIENCY

Time and space complexity

Time complexity

O(1) random access

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.