Create three variables for storing Student’s name, semester and Cgpa. Take input form the user in the variables.
Write the students data in file named Student.txt.
Open the file “Student.txt”, display an error if the file is not found. If the file is found, open it and read the
Students data and display on screen
.
#include <stdio.h>
#include <stdlib.h>
//Main Start
int main() {
//Variable Declaration
char line[1000],student_name[50];
FILE *fptr,*f1;
int student_semester;
float student_cgpa;
//Input from user
printf("Enter Student Details \n");
printf("Enter Name of Student: \n");
scanf("%s", &student_name);
printf("Enter Semester: \n");
scanf("%d", &student_semester);
printf("Enter CGPA: \n");
scanf("%f", &student_cgpa);
//append text file with input data
f1=fopen("student.txt","a+");
fprintf(f1,"%s %d %.2f \n",&student_name,student_semester,student_cgpa);
fclose(f1);
if ((fptr = fopen("student.txt", "r")) == NULL) {
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
printf("=========================");
printf("Student Details (Name, Semester and CGPA");
// reads text until newline is encountered
while (fgets(line, sizeof(line), fptr)) {
printf("%s", line);
}
fclose(fptr);
return 0;
}
//End Main
Comments
Leave a comment