Skip to content

Exercise1 #136

@Vin182032

Description

@Vin182032

Here are the C programs for the tasks you mentioned:

  1. C program to read name and marks of 5 students and store them in a file:
#include <stdio.h>

struct Student {
    char name[50];
    int marks;
};

int main() {
    struct Student students[5];
    FILE *file = fopen("students.txt", "w");

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    for (int i = 0; i < 5; i++) {
        printf("Enter name of student %d: ", i + 1);
        scanf("%s", students[i].name);
        printf("Enter marks of student %d: ", i + 1);
        scanf("%d", &students[i].marks);
        fprintf(file, "%s %d\n", students[i].name, students[i].marks);
    }

    fclose(file);
    printf("Data written to file successfully.\n");
    return 0;
}
  1. C program to read name and marks of n students from the user and add to an existing file:
#include <stdio.h>

struct Student {
    char name[50];
    int marks;
};

int main() {
    struct Student student;
    FILE *file = fopen("students.txt", "a"); // Open in append mode

    if (file == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    int n;
    printf("Enter number of students to add: ");
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        printf("

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions