5 Computer -- Programming II

Write a C program to enter name, roll number and percentage of 10 students and store them in a data file [use any name].

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;

}


More questions on Programming II

Differentiate between structure and union.

Structures in Cis 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 astructure, you must use thestructstatement. The struct statement defines a newdata 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; }; (OR) struct [structure name] { member...
Close Open App