I have the following code (shown below) compiling correctly and I'm able to run it. The only thing I need to add is logic so that if there is no input data file in the directory, to display a message "Error Opening File". How can I do this?
This code looks for an input_data.txt file, then it calculates something and writes to another file called output_data.txt file.
Code:
int main ()
{
FILE *fptr1;
FILE *fptr2;
char fName[20];
char lName[20];
char buf[100];
char letterGrade;
int final_score;
int count;
int quiz1, quiz2, quiz3, quiz4, mid, final;
fptr1 = fopen("input_data.txt","r");
//display the results
fptr2 = fopen("output_data.txt","w");
//reading using fscanf
while(fscanf(fptr1,"%s %[^ \t\n\r\v\f,]%*c %d, %d, %d, %d, %d, %d", fName,lName,&quiz1,&quiz2,&quiz3,&quiz4,&mid,&final) != EOF)
{
//determine final_score calculation
final_score = quiz1*.10 + quiz2*.10 + quiz3*.10 + quiz4*.10 + mid*.25 + final*.35;
printf("%d", final_score);
//determine letter grade based on final_score
if (final_score >= 90)
letterGrade = 'A';
else if (final_score >= 80 && final_score < 90)
letterGrade = 'B';
else if (final_score >= 70 && final_score < 80)
letterGrade = 'C';
else if (final_score >= 60 && final_score < 70)
letterGrade = 'D';
else
letterGrade = 'F';
//following code will output the file
//and move on to the second student
if(fptr2)
{
fprintf(fptr2, "%s %s :%c\n", fName, lName, letterGrade);
fflush(fptr2);
}
}
fclose(fptr1);
fclose(fptr2);
}