C Programming: Create a struct named “Student” along with 3 properties, Name, ID, Score Choosing the correct data type for each property. Once the program is running, it will check if the student.bin file is exist and if it is not exist, the program will ask user to enter three student information and save them to file student.bin . If the file is exist, read the second student information and print it in console.
#include<stdio.h>
struct student {
int id;
int score;
char name[20];
}s1,s2,s3; // struct student is created
int main() {
FILE *fptr;
int i = 1;
struct student out;
fptr = fopen("student.bin","r"); //opening the file in
read mode
if(fptr == NULL) {
fptr = fopen("student.bin","w"); //
if the file doesnot exists opening the file in write mode
printf("Enter student1 information
in order name id score\n");
scanf("%s
%d%d",s1.name,&s1.id,&s1.score);
printf("Enter student2 information
in order name id score\n");
scanf("%s
%d%d",s2.name,&s2.id,&s2.score);
printf("Enter student3 information
in order name id score\n");
scanf("%s
%d%d",s3.name,&s3.id,&s3.score); //writing the student
information in a file
fwrite(&s1,sizeof(struct
student),1,fptr);
fwrite(&s2,sizeof(struct
student),1,fptr);
fwrite(&s3,sizeof(struct
student),1,fptr);
fclose(fptr);
} else {
printf("File Exists\nThe 2nd
student information\n");
while(fread(&out,sizeof(struct
student),1,fptr)) {
if(i == 2)
{
printf("Name = %s ID = %d Score =
%d",out.name,out.id,out.score); // if then file exists printing the
second student inforrmation
}
i++;
}
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.