Question

In: Computer Science

Write a C Program that uses file handling operations of C language. The Program should perform...

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; }

Solutions

Expert Solution

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;

}


Related Solutions

C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
write a program in C language Create a function to perform the insertion sort. Now the...
write a program in C language Create a function to perform the insertion sort. Now the program will perform the following steps: Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Use dynamic memory allocation to allocate an array of N single precision floating-point elements (C type float). Read the N single precision floating-points elements to the allocated array. Invoke a function to sort the array using insertion sort (the insertion...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; else return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results Submit your code and report here.
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT