Hi. Let me preface this by saying I am not asking anyone to do my homework for me, just looking for someone to help steer me in the right direction. I will post the description of the assignment and then post the code I have thus far. I know my code doesn't work(it crashes right away) but hopefully it will let you see how I am approaching this program and then you can feel free to tell me how awful my program is:-). Seriously though, I welcome any constructive criticism and again, I am not asking anyone to do my work for me but hopefully you can tell me which parts of my program are working and which I need to examine a little more closely. Thanks in advance.
And here is my code so farCode:Title: Lab 14
Description: Simple File Input and Output
Points: 100
Source Code File Name: <G-Number>-lab14.c
Subject Key: CS133U Lab 14 Submission
Standard Input:
Input file name as command line argument argv[1]
Input file contains three floating values and an operator, e.g.
2.5
3.5
4.5
+
Introduction
------------
This assignment extends input and output to file streams. You have used printf and scanf
in earlier assignments, and in this lab you should use the more general fscanf and fprintf
functions to read and write to files.
Your program will accept the name of an input file from the command line, specifically argv[1].
If you are using DevC++ then you will need to set the name of the input file in the Execute/Parameters
dialog box. You may use any valid file name you wish, but the file must have the format shown in the
"Standard Input" section, i.e. three float values followed by a char value (a total of four lines
in the file). Keep in mind that when I grade your program I will use my own file name, so do not
hard-code a file name into your code - use the name specified in argv[1].
Your program will read the numbers and the operator from the input file, and then display the sum of
the values. Please note that while you must read the operator character from the file, you do NOT
need to interpret the operator character. You can simply add the three values no matter what operation
is specified.
After displaying the sum of the three values your program will write "Hello World!" to an output
file called "lab14.output".
Instructions
============
1. Copy the 'hdrm.c' file to 'lab14.c' and use it as the starting point for your code. Update the
comments in the file to reflect this assignment.
2. The command line argument argv[1] contains the input file name. Open this file for input.
3. Read the 4 lines of the input file (3 float values and 1 char value) and close the file.
4. Display the values you read
5. Display the sum of the 3 float values with a precision of 3 decimal places
6. Open the file 'lab14.output' for output
7. Write the string "Hello World!" to the output file 'lab14.output' and close the output file
Submission
==========
Your source code file must be named <G-Number>-lab14.c, and must be an ASCII text file.
You should submit just the source code file - no executable code!
I will compile and execute your source code on my grading system and return a
feedback file to you in a few days.
You will submit your <G-Number>-lab14.c source code file using Blackboard,
as discussed in class. Submit your code to the Lab 4 assignment dropbox.
Standard Input
==============
13:27:16> cat lab14.input
2.5
3.5
4.5
+
Sample Output
=============
13:27:19> lab14.exe lab14.input
Read the following values from 'lab14.input':
2.500
3.500
4.500
2.500 + 3.500 + 4.500 = 10.500
Press ENTER to end
[normal program end]
13:27:38> cat lab14.output
Hello World!
13:27:43>
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, str[30]; //Character and String variable
FILE *fp; //File pointer
//Prompt to enter filename
printf("Enter filename: \n");
//Opens and reads file specified in argv[1]
fp = fopen(argv[1], "r");
//Scans values from specified file
fscanf(fp, "%f %f %f %c", &val_A, &val_B, &val_C, &ch_A);
//Stores filename to string variable
str[30] = fp;
//Calculates the sum of the three values in specified file
sum = val_A + val_B + val_C;
//Displays error message if file does not exist
if (fp == NULL)
printf("Not a valid file\n");
//Displays values and the sum of the values from the specified file
else if (fp != NULL){
printf("Read the following values from '%s'\n", str);
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);
}
void main_end()
{
printf("Press ENTER to end");
fgetc(stdin);
printf("[normal program end]\n");
exit(0);
}