Structure File Record
Structure File Record
Takes multiple records from the user and writes them to a file.
Reads those records back from the file and displays them.
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
FILE *fp;
struct Student s;
int n, i;
// Writing records
fp = fopen("students.dat", "wb"); // open in binary write
mode
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fclose(fp);
printf("\nRecords written successfully.\n");
// Reading records
fp = fopen("students.dat", "rb"); // open in binary read
mode
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fclose(fp);
return 0;
}
Sample input/output
ID: 101
Name: Alice Johnson
Marks: 88.50
ID: 102
Name: Bob Smith
Marks: 92.00