Hey Grade 12 Students, your exams are near so work hard.

Write a C program to write and read symbol number, name and percentage of any 10 students to/from data file "students.txt".

#include <stdio.h>
#include <stdlib.h>

struct student {
    int symbol_number;
    char name[50];
    float percentage;
};

int main() {
    FILE *fp;
    int i;

    // create an array of 10 students
    struct student students[10];

    // prompt the user to enter details for each student
    printf("Enter details of 10 students:\n");
    for (i = 0; i < 10; i++) {
        printf("Student %d:\n", i+1);
        printf("Symbol number: ");
        scanf("%d", &students[i].symbol_number);
        printf("Name: ");
        scanf("%s", students[i].name);
        printf("Percentage: ");
        scanf("%f", &students[i].percentage);
    }

    // write student data to file
    fp = fopen("students.txt", "w");
    if (fp == NULL) {
        printf("Error opening file.\n");
        exit(1);
    }
    for (i = 0; i < 10; i++) {
        fprintf(fp, "%d %s %f\n", students[i].symbol_number, students[i].name, students[i].percentage);
    }
    fclose(fp);

    // read student data from file
    fp = fopen("students.txt", "r");
    if (fp == NULL) {
        printf("Error opening file.\n");
        exit(1);
    }
    printf("\nStudent data read from file:\n");
    for (i = 0; i < 10; i++) {
        fscanf(fp, "%d %s %f", &students[i].symbol_number, students[i].name, &students[i].percentage);
        printf("Symbol number: %d, Name: %s, Percentage: %.2f\n", students[i].symbol_number, students[i].name, students[i].percentage);
    }
    fclose(fp);

    return 0;
}

Getting Info...

About the Author

A free online educational resource provider.

Post a Comment

Please do not enter any spam link in the comment box.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.