Question: Write a program that reads 10 integers from a file, stores the integers in an array, sorts, and prints the values in the array. You
Write a program that reads 10 integers from a file, stores the integers in an array, sorts, and prints the values in the array.
You should have the following functions:
1. A function that reads the integer values from a file and stores the integers in an array
2. A function that sorts an array of 10 integers
3. A function that prints an array of 10 integers.
Below are a few of the steps you will need:
1. Create a variable of type FILE that is a pointer (FILE *).
2. Open the file for reading, the file name will be supplied on the command line.
3. Validate the file opened appropriately.
4. Create an integer array of size 10 initializing all values to 0.
5. Call the necessary functions to read, sort, and print the data.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include
#include
int main(int argc, char* argv[])
{
FILE *ifp, *ofp;
int num;
if(argc < 3)
{
printf("Usage Error:
exit(1);
}
ifp = fopen(argv[1], "r");
if (ifp == NULL)
{
printf("Can't open input file %s! ", argv[1]);
exit(1);
}
ofp = fopen(argv[2], "w");
if (ofp == NULL)
{
printf( "Can't open output file %s! ", argv[2]);
exit(1);
}
fscanf(ifp, "%i", &num);
fprintf(ofp, "%i", num);
fclose(ifp);
fclose(ofp);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
