- Asked 2/16
- 2071 Chaitra · 7 marks
- 2070 Asar · 8 marks
Read records until end; show nth record
Short answer
Define a struct for worker details. Write records to a binary file using fwrite until user stops. To read the nth record, use fseek to (n-1)*sizeof(struct) and fread. Validate n against record count.
Write a program to continuously read name, age and salary of a worker and write it into a file until user confirms to end. Then read n from user and display the nth record in the file. Details of worker must be represented by a structure. (Variant: details of book authors with name, nationality and number of books published.)
Design. The worker's details are held in struct worker (name, age, salary). Records are written to a binary file worker.dat with fwrite() until the user answers n to "Add another record?". Because every record has the same size, the n-th record starts at byte (n − 1) × sizeof(struct worker). fseek() jumps there and fread() reads it. A check on n prevents reading outside the file.
(For the variant, replace the members by author name, nationality and number of books.)
#include <stdio.h>
struct worker {
char name[30];
int age;
float salary;
};
int main(void)
{
FILE *fp;
struct worker w;
char more;
int n, count = 0;
fp = fopen("worker.dat", "wb");
if (fp == NULL) {
printf("Cannot open file\n");
return 1;
}
do {
printf("Enter name age salary: ");
scanf("%29s %d %f", w.name, &w.age, &w.salary);
fwrite(&w, sizeof(w), 1, fp);
count++;
printf("Add another record (y/n)? ");
scanf(" %c", &more);
} while (more == 'y' || more == 'Y');
fclose(fp);
printf("Enter n: ");
scanf("%d", &n);
if (n < 1 || n > count) {
printf("No such record (file has %d records)\n", count);
return 1;
}
fp = fopen("worker.dat", "rb");
if (fp == NULL) {
printf("Cannot open file\n");
return 1;
}
fseek(fp, (long)(n - 1) * sizeof(w), SEEK_SET); /* jump to nth record */
fread(&w, sizeof(w), 1, fp);
printf("Record %d: %s %d %.2f\n", n, w.name, w.age, w.salary);
fclose(fp);
return 0;
}
Sample run (three workers entered, then n = 2)
Input: Ram 30 12000 y Sita 25 15000 y Hari 41 9000 n 2
Enter name age salary: Add another record (y/n)? Enter name age salary: Add another record (y/n)? Enter name age salary: Add another record (y/n)? Enter n: Record 2: Sita 25 15000.00
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Display records sorted by name from file · Computer Programming, chapter 10
- Sort employee structures by salary · Computer Programming, chapter 8 · asked 2×
- College records with nested date structure · Computer Programming, chapter 8
- Employee text file; sort into binary file · Computer Programming, chapter 10
- nth Fibonacci term prime check · Computer Programming, chapter 6
- Employees with salary above 10,000 · Computer Programming, chapter 8
- Student records file with CRN search · Object Oriented Programming, chapter 8
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Write and display 10 employees using file · Object Oriented Programming, chapter 8
- Paired test: safety program worker hours · Probability and Statistics, chapter 6 · asked 3×