Answer to Question #177459 in C++ for Talha

Question #177459

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

.




1
Expert's answer
2021-04-01T02:49:44-0400
#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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS