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.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
102 Ravi 91
COMPLETE C PROGRAM
Complete C implementation
#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;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
102 Ravi 91
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use equal-size binary records so byte offset equals record index times record size.
- 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.
