In: Computer Science
Write a C Program that uses file handling operations of C
language. The Program should perform following operations:
1. The program should accept student names and students’ assignment
marks from the user.
2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file that gets generated by your program by both the spreadsheet application like MS-Excel and also with a normal text editor like Notepad.
3. The program should further read data stored in the file created in Step 2; and then create a report in text file (with .txt extension) such that the report shows average marks obtained by each student. Also, the report should show the average marks in each assignment obtained in the class.
Sample Report file, “Report.txt” has been uploaded. You may
achieve better formatting than this one!!
Part of the Program has been given here. Last part where the report
calculates the average marks in each assignment obtained by all
students has NOT been provided and is expected to be completed by
the students.
/*Program to accept Student Assignment Marks from the user and
store them in a CSV File. Then read data from this file and create
a Students' Assignment Statistics Report */
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
struct student_grades
{
char name[20];
int assign1;
int assign2;
int assign3;
};
int num_students = 0;
if ((fptr1 = fopen("Grades.csv","w")) == NULL)
printf("\n Error opening File");
printf("\nHow many students are there?: ");
scanf("%d",&num_students);
fflush(stdin);
printf("\n----------------------Input data----------------------------------\n");
printf("----------- Student Name\tAssign#1\tAssign#2\tAssign#3 -------\n");
printf("--------------------------------------------------------------------\n");
for(int i=0;i<num_students;i++)
{
struct student_grades sgrad;
printf("\nStudent %d. ",i+1);
fscanf(stdin,"%s\t%d\t%d\t%d",sgrad.name,&sgrad.assign1,&sgrad.assign2,&sgrad.assign3);
fprintf(fptr1,"%s , %d , %d , %d\n",sgrad.name,sgrad.assign1,sgrad.assign2,sgrad.assign3);
}
fclose(fptr1);
if ((fptr1 = fopen("Grades.csv","r")) == NULL)
printf("\n Error opening File");
if ((fptr2 = fopen("Report.txt","w")) == NULL)
printf("\n Error opening File");
fprintf(fptr2," Students' Assignment Statistics\n");
fprintf(fptr2,"\n--- Name \t Total marks \t Average ------\n");
for(int i=0;i<num_students;i++)
{
char line[100]; // Used by commented-out code
int tot_marks = 0;
float marks_average = 0;
struct student_grades sgrad;
char comma = ','; //Following commented-out code is alternate code for
fscanf() on the following line
//fgets(line,100,fptr1);
//sscanf(line,"%s %c %d %c %d %c %d",sgrad.name, &comma,&sgrad.assign1,&comma,&sgrad.assign2,&comma,&sgrad.assign3);
fscanf(fptr1,"%s %c %d %c %d %c %d",sgrad.name, &comma,&sgrad.assign1,&comma,&sgrad.assign2,&comma,&sgrad.assign3);
tot_marks = sgrad.assign1+sgrad.assign2+sgrad.assign3;
marks_average = (float)tot_marks/3;
fprintf(fptr2,"%s \t\t %d \t\t %0.2f\n",sgrad.name,tot_marks,marks_average);
}
rewind(fptr1); /* This part of the code to calculate the Average of
Class in each Assignment has to be completed by students. */
fclose(fptr1);
fclose(fptr2);
return 0; }
If you have any doubts, please give me comment...
/*Program to accept Student Assignment Marks from the user and store them in a CSV File. Then read data from this file and create a Students' Assignment Statistics Report */
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
struct student_grades
{
char name[20];
int assign1;
int assign2;
int assign3;
};
int num_students = 0;
if ((fptr1 = fopen("Grades.csv", "w")) == NULL)
printf("\n Error opening File");
printf("\nHow many students are there?: ");
scanf("%d", &num_students);
fflush(stdin);
printf("\n----------------------Input data----------------------------------\n");
printf("----------- Student Name\tAssign#1\tAssign#2\tAssign#3 -------\n");
printf("--------------------------------------------------------------------\n");
for (int i = 0; i < num_students; i++)
{
struct student_grades sgrad;
printf("\nStudent %d. ", i + 1);
fscanf(stdin, "%s\t%d\t%d\t%d", sgrad.name, &sgrad.assign1, &sgrad.assign2, &sgrad.assign3);
fprintf(fptr1, "%s , %d , %d , %d\n", sgrad.name, sgrad.assign1, sgrad.assign2, sgrad.assign3);
}
fclose(fptr1);
if ((fptr1 = fopen("Grades.csv", "r")) == NULL)
printf("\n Error opening File");
if ((fptr2 = fopen("Report.txt", "w")) == NULL)
printf("\n Error opening File");
fprintf(fptr2, " Students' Assignment Statistics\n");
fprintf(fptr2, "\n--- Name \t Total marks \t Average ------\n");
for (int i = 0; i < num_students; i++)
{
char line[100]; // Used by commented-out code
int tot_marks = 0;
float marks_average = 0;
struct student_grades sgrad;
char comma = ','; //Following commented-out code is alternate code for fscanf() on the following line
//fgets(line,100,fptr1);
//sscanf(line,"%s %c %d %c %d %c %d",sgrad.name, &comma,&sgrad.assign1,&comma,&sgrad.assign2,&comma,&sgrad.assign3);
fscanf(fptr1, "%s %c %d %c %d %c %d", sgrad.name, &comma, &sgrad.assign1, &comma, &sgrad.assign2, &comma, &sgrad.assign3);
tot_marks = sgrad.assign1 + sgrad.assign2 + sgrad.assign3;
marks_average = (float)tot_marks / 3;
fprintf(fptr2, "%s \t\t %d \t\t %0.2f\n", sgrad.name, tot_marks, marks_average);
}
rewind(fptr1); /* This part of the code to calculate the Average of Class in each Assignment has to be completed by students. */
double assign1_avg =0, assign2_avg = 0, assign3_avg = 0;
for (int i = 0; i < num_students; i++)
{
struct student_grades sgrad;
char comma = ',';
fscanf(fptr1, "%s %c %d %c %d %c %d", sgrad.name, &comma, &sgrad.assign1, &comma, &sgrad.assign2, &comma, &sgrad.assign3);
assign1_avg += sgrad.assign1;
assign2_avg += sgrad.assign2;
assign3_avg += sgrad.assign3;
}
assign1_avg /= num_students;
assign2_avg /= num_students;
assign3_avg /= num_students;
fprintf(stdout, "Average marks of Assignment-1: %.2lf\n", assign1_avg);
fprintf(stdout, "Average marks of Assignment-2: %.2lf\n", assign2_avg);
fprintf(stdout, "Average marks of Assignment-3: %.2lf\n", assign3_avg);
fclose(fptr1);
fclose(fptr2);
return 0;
}