-
Notifications
You must be signed in to change notification settings - Fork 122
Open
Description
Here are the C programs for the tasks you mentioned:
- 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;
}- 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("Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels