Combined Example
This example uses command line arguments to give the program the name
of the input file to use and the name of the output file to write during
processing. This is a very common use of command line arguments.
The Program
/**********************************************
* File: combo.c
* Author: S. Bogar
* Date: ??
* SSN: 123-45-6789
* Section: 0101
* Email: bogar@cs.umbc.edu
*
* A program to demonstrate the use of command-line
* arguments. It demonstrates how to open files and
* do some error-checking.
*
* This program also reads information from the input file,
* dynamically allocates memory to hold an array of TRIANGLE
* structures, reads in the values of all sideAs and sideBs,
* calculates the hypotenuse for each right triangle, and
* prints the results into the output file.
*
* This program requires command line arguments that are
* the name of the input file and the name of the output
* file to be used.
***********************************************/
#include
#include
#include
typedef struct triangle
{
float sideA;
float sideB;
float hypotenuse;
} TRIANGLE;
int main(int argc, char* argv[])
{
int i, items;
TRIANGLE *triangles;
FILE *ifp, *ofp;
/* Make sure there were 3 items typed on the command line */
if (argc != 3)
{
printf ("This program requires command line arguments\n");
printf ("that are the name of the input file to be\n");
printf ("used, and the name of the output file to\n");
printf ("be used\n");
exit (-1);
}
/* Open the input file for reading*/
ifp = fopen (argv[1], "r");
if (ifp == NULL)
{
printf("Sorry, couldn't open input file:\n");
printf("%s\n", argv[1]);
exit (-2);
}
/* Open the output file for writing */
ofp = fopen (argv[2], "w");
if (ofp == NULL)
{
printf("Sorry, couldn't open output file:\n");
printf("%s\n", argv[2]);
exit (-3);
}
/* read the number of items from the file */
fscanf(ifp, "%d", &items);
/* Get a block of memory big enough to hold that many TRIANGLES */
triangles = (TRIANGLE *) malloc(items * sizeof(TRIANGLE)) ;
if(triangles == NULL)
{
printf("Not enough memory\n");
exit(-4);
}
/* Lets use it as an array of TRIANGLES and fill the values
of all the sideAs and sideBs by reading them from the file */
for(i = 0; i < items; i++)
{
fscanf(ifp, "%f", &triangles[i].sideA);
fscanf(ifp, "%f", &triangles[i].sideB);
}
/* Close the input file since we're finished reading from it */
fclose(ifp);
/* Calculate the hypotenuse */
for (i = 0 ; i < items ; i++)
{
triangles[i].hypotenuse = sqrt(triangles[i].sideA *
triangles[i].sideA +
triangles[i].sideB *
triangles[i].sideB );
}
/* Print out the results */
fprintf(ofp, "\n\tSide A\t\tSide B\t\tHypotenuse\n\n");
for (i = 0 ; i < items ; i++)
{
fprintf(ofp, "\t%6.3f\t\t%6.3f\t\t%6.3f\n", triangles[i].sideA,
triangles[i].sideB, triangles[i].hypotenuse);
}
fprintf(ofp, "\n");
/* Close the output file since we're finished writing to it */
fclose(ofp);
/* Give up use of the memory block */
free(triangles) ;
return 0;
}
The Input File
linux1[520] cat triangles.dat
5
3.000 4.000
3.250 5.750
4.500 7.250
5.000 8.000
6.000 8.000
linux1[521]
The Sample Run
linux1[626] a.out triangles.dat triangles.out
linux1[627]
The Output File
linux1[627] more triangles.out
Side A Side B Hypotenuse
3.000 4.000 5.000
3.250 5.750 6.605
4.500 7.250 8.533
5.000 8.000 9.434
6.000 8.000 10.000
Last Modified -