Write a C program to enter name, roll number and percentage of 10 students and store them in a data file [use any name].
#include <stdio.h>
struct student {
char name[50];
int rollno;
float percentage;
};
int main() {
struct student s[10];
int i;
printf("Enter details of 10 students:\n");
for (i = 0; i < 10; i++) {
printf("\nStudent %d:\n", i+1);
printf("Name: ");
scanf("%s", s[i].name);
printf("Roll Number: ");
scanf("%d", &s[i].rollno);
printf("Percentage: ");
scanf("%f", &s[i].percentage);
}
// Open the file for writing
FILE *fp;
fp = fopen("students.dat", "wb");
// Write the details of the 10 students to the file
fwrite(s, sizeof(struct student), 10, fp);
// Close the file
fclose(fp);
return 0;
}
Structures in C is a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.
Defining a structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than or equal to one member. The format of the struct statement is as follows:
struct [structure name] { member definition; member definition; ... member definition;...