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

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...
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...
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.
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...
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
In the C# programming language... Write a program to perform student record manage for class IST311....
In the C# programming language... Write a program to perform student record manage for class IST311. Create student record class (Student.cs) and it should get the student information from the user and set up student record class. The program will perform recording student’s grade information and compute final grade, and then print out the student’s class information. Student class definitions: It should contain attributes as following: first name, last name, 5 labs grade, 3 grade, final grade. Its member functions...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT