Please help me write this c program with comments Use struct
Please help me write this c program with comments.
Use structs and file I/O to separate a large assignment feedback text file into individualized
feedback files, one per student.
Input File Format
• The large assignment feedback text file has the following format for the lines in each
student entry:
o Student name
o “Feedback on “ followed by the assignment name
o “Mark:” followed by a space, the numeric mark, a colon, a space, and the generic
mark description
o “Comments:”
o No more than ten comments, with each comment starting with an asterisk and a
space and being on a separate line from the next comment
o \"END\"
• An example of a student entry is:
Flintstone, Fred
Feedback on Assignment 2
Mark: 80: Meets or almost meets expectations.
Comments:
* Your program exits on any value above 3.
* Your file header comment was missing your name.
* Prototypes incomplete.
END
• Your program must be able to handle any number of student entries in a file.
• Individual input lines will not exceed 400 characters.
Functional Requirements
• Your program must go through the input file, extracting each student entry, and create
one output file for each student\'s feedback information.
o Each of the output files must be a text file with a \".txt\" extension. The base part
of the filename before the extension must be the student\'s name. Thus, if the
student\'s name is \"Van Gogh, Vincent\", the output filename containing his
information will be \"Van Gogh, Vincent.txt\".
o For example, if the input file contains feedback for three students named \"Van
Gogh, Vincent\", \"Shakespeare, William\", and \"Churchill, Winston\", there will be
three output files with names of \"Van Gogh, Vincent.txt\", \"Shakespeare,
William.txt\", and \"Churchill, Winston.txt\".
• Each output file must have information in the exact format and with the same content as
found in the input file with the following exceptions:
o Each comment will be indented by one TAB (use \'\\t\' in your printf format string
for this).
o If there are any comments that do not have any content besides the asterisk or the
asterisk and a space, omit that content.
o Omit the final \"END\".
• An example of the output of a student entry is:
Flintstone, Fred
Feedback on Assignment 2
Mark: 80: Meets or almost meets expectations.
Comments:
* Your program exits on any value above 3.
* Your file header comment was missing your name.
* Prototypes incomplete.
• The input file must be called \"feedback.txt\". It will be found in the current directory.
o It is your responsibility to figure out exactly what Visual Studio considers \"the
current directory\" to be.
• The output files are all put in the current directory.
Other Requirements
• Error checking is required for the fopen() call but is not required for other function calls
(this will come later in the Project). If fopen() fails, display an error message and quit the
program.
• It must not use global variables, goto, or exit(). It must not take in user input.
• Each student entry must be stored in a struct that you create (failing to do this will result
in a failing mark in the assignment). The struct must contain fields for:
o Name (as a null-terminated string)
o Assignment name (as a null-terminated string)
o Mark information (as a null-terminated string)
o Array of ten comments (as a two-dimensional array of null-terminated strings)
• Do not write to the output file until the struct is completely full for the particular student.
NOTES:
• You can assume that the input file will always meet the above specifications.
• It is vitally important that you follow the above requirements.
Solution
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max_line_size 400 // Since Each line consists of maximum of 400 characters including newline character
struct student
{
char name[401];//1 Byte extra to hold the \'\\0\'
char feedback[401];
char mark[401];
char comments[10][401];
};//Declaring Structure to hold the mentioned Data
int main()
{
FILE *feedback_file = fopen(\"feedback.txt\",\"r\");//Opening the feedback file in readmode
if(!feedback_file)//Checking the error condition while opening the feedback file
{
printf(\"Error opening feedback.txt file....\ \");
return 1;//Exit from the program if error is there
}
struct student temp;//Defining the variable to temporarily hold the data of each student
while(fgets(temp.name,max_line_size+1,feedback_file))//Iterating the file till end of EOF
{
int name_length = strlen(temp.name);
if(temp.name[name_length-1] == \'\ \')//Eliminating the newline character from student name since we should use \ character in the individual student file name
{
temp.name[name_length-1] = \'\\0\';
}
fgets(temp.feedback,max_line_size+1,feedback_file);//Fetching the Feedback line
fgets(temp.mark,max_line_size+1,feedback_file);//Fetching the mark line
fseek(feedback_file,10,SEEK_CUR);//Skipping the \"Comments:\ \"
int i;
for(i=0;i<10; i++)//Fetching the comments line by line
{
fgets(temp.comments[i],max_line_size+1,feedback_file);
if(!strcmp(temp.comments[i],\"END\ \"))//Skipping \"END\ \" string
{
i--;
break;
}
}
name_length = strlen(temp.name);
char *file_name = malloc(name_length+4);
strcpy(file_name,temp.name);
strcpy(file_name+name_length,\".txt\");//Creating the \"individualfile.txt\" string
FILE *individual_file = fopen(file_name,\"w\");//Creating the file and opening in writemode
if(!individual_file)//Error checking
{
printf(\"Error Creating %s file\",file_name);
return 1;//Exit from the program if error occurs
}
fputs(temp.name,individual_file);//Putting the student name into individual file
fputc(\'\ \',individual_file);//adding new line character since we removed newline character initially
fputs(temp.feedback,individual_file);//Putting feedback string
fputs(temp.mark,individual_file);//putting mark string
fputs(\"Comments:\ \",individual_file);//putting \"Comments:\ \"
int j;
for(j=0;j<=i;j++)//Putting comments
{
if(strlen(temp.comments[j])>3)
fputs(temp.comments[j],individual_file);
}
}
return 0;
}


