ADVANCED DATA STRUCTURES PROGRAM • LEVEL 19 — FILES AND FILE ORGANIZATION
Search a Sequential Record File
Learn how to search a sequential record file using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Found = B
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
struct Record{int id;char name[8];};
int main(void)
{
struct Record records[]={{10,"A"},{20,"B"},{30,"C"}};FILE*file=fopen("sequential.bin","wb");if(!file)return 1;fwrite(records,sizeof records[0],3,file);fclose(file);file=fopen("sequential.bin","rb");struct Record value;while(fread(&value,sizeof value,1,file)==1)if(value.id==20){printf("Found = %s\n",value.name);break;}fclose(file);return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Found = B
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Read records in storage order until the requested key is encountered.
- Display the computed result.
Read records in storage order until the requested key is encountered.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
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.
