Thanks for the help so far. I am starting to close in on this but I am still having some trouble. I've changed some things around and can get the program to run as intended when I hard code in a file name, but I am still having problems when I try to use a file name specified in argv[1]. I know it seems like I am just continuing to ask questions until someone gives me the correct code, but I assure you I am only using the forum as a last resort and am only wanting guidance, not the answer. Thanks again in advance.
Code:
/**
@page lab14 Simple Input and Output
This program accepts the name of an input file, reads numbers and operators
from the input file and then dispalys the sum of the numbers.
@section Authors
@author Justin Foss
<p>
@section Contributors
- N/A
*/
/**
@file G03426789.c
@version 1.0
@details
Demonstrate input and output using fscanf and fprintf
*/
#include <stdio.h>
#include <stdlib.h>
void main_end();
int main(const int argc, const char **argv)
{
float val_A, val_B, val_C, sum; //Float variable
char ch_A; //Character variable
FILE *fp; //File pointer
fp = argv[1];
//Displays error message if file does not exist
if (fp == NULL){
printf("Not a valid file\n");
exit(1);
}
//Scans values from specified file
else if (fp != NULL){
fp = fopen(argv[1], "r");
fscanf(fp, "%f %f %f %c", &val_A, &val_B, &val_C, &ch_A);
sum = val_A + val_B + val_C;
printf("Read the following values from %s\n", argv[1]);
printf("\t%.3f\n \t%.3f\n \t%.3f\n", val_A, val_B, val_C);
printf("\n%.3f + %.3f + %.3f = %.3f", val_A, val_B, val_C, sum);
}
//Closes file
fclose(fp);
fgetc(stdin);
}
void main_end()
{
printf("Press ENTER to end");
fgetc(stdin);
printf("[normal program end]\n");
exit(0);
}