Created
January 15, 2021 18:08
-
-
Save muhammedfurkan/093871d3f9d45eb29e0f85935371feed to your computer and use it in GitHub Desktop.
Write a C program that saves the name,surname,midterm grade,final grade information of 10 students entered on the keyboard into a file named "notes.txt"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define BUFFER_SIZE 500 | |
int main() | |
{ | |
// buffer to store data in buffer between writing to file | |
char buffer[BUFFER_SIZE]; | |
char ch; // To store single character | |
int numStudents; | |
// Student details | |
char name[50]; | |
char surname[50]; | |
int midGrades; | |
int finalGrades; | |
// File Pointer | |
FILE *file; | |
// Open notes.txt in write mode | |
file = fopen("notes.txt", "w"); | |
// If error in opening file | |
if (file == NULL) | |
{ | |
printf("Error in opening file."); | |
// Exit from code | |
return -1; | |
} | |
// Ask how many students details user want to enter | |
printf("Enter the no. of students: "); | |
scanf("%d", &numStudents); | |
printf("Enter student details: \n"); | |
for (int i = 0; i < numStudents; i++) | |
{ | |
printf("\nStudent: %d\n", (i + 1)); | |
printf("Enter name: "); | |
scanf("%s", &name); | |
printf("Enter surname: "); | |
scanf("%s", &surname); | |
printf("Enter midGrades: "); | |
scanf("%d", &midGrades); | |
printf("Enter finalGrades: "); | |
scanf("%d", &finalGrades); | |
// Write to file delimeted by "," (comma) | |
fprintf(file, "%s,%s,%d,%d\n", name, surname, midGrades, finalGrades); | |
} | |
// Close the file | |
fclose(file); | |
// Open file again in read mode | |
file = fopen("notes.txt", "r"); | |
// Get data from file to buffer | |
printf("\n---- Results ----\n\n"); | |
while (fgets(buffer, BUFFER_SIZE, file)) | |
{ | |
char *token; | |
if (buffer != NULL) | |
{ | |
int i = 1; | |
char *str = buffer; | |
// Split the string by " " | |
while (token = strtok_r(str, ",", &str)) | |
{ | |
if (i == 1) | |
{ | |
// Get name | |
strcpy(name, token); | |
} | |
else if (i == 2) | |
{ | |
// Get surname | |
strcpy(surname, token); | |
} | |
else if (i == 3) | |
{ | |
// Get midGrades marks | |
midGrades = atoi(token); | |
} | |
else if (i == 4) | |
{ | |
// Get finalGrades marks | |
finalGrades = atoi(token); | |
} | |
i++; | |
} | |
// Check if student is passed | |
// Takeout 40% of midGrades and 60% of finalGrades | |
midGrades -= (midGrades * 40) / 100; | |
finalGrades -= (finalGrades * 60) / 100; | |
// Get sum of midGrades and finalGrades | |
int sum = midGrades + finalGrades; | |
// Print output | |
printf("Name = %s %s New Final Marks = %d", name, surname, sum); | |
if (sum >= 50) | |
{ | |
printf(" Result: Passed\n"); | |
} | |
else | |
{ | |
printf(" Result: Failed\n"); | |
} | |
} | |
} | |
// Close the file | |
fclose(file); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment