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 }
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.
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...
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT