I am writing a C program that reads integersfrom a text file whose name is given
ID: 3612656 • Letter: I
Question
I am writing a C program that reads integersfrom a text file whose name is given as a command-line argument.The program will display the largest, smallest, and median integerfromthe file.I have some partial code written to open the file and read thefile, plus I already have the sorting function ready to peice intothe program. I do need help sorting out the "file name given as acommand line argument"
Also can I just call my sort fuction like normal in main(), callingthe function and sending the name of the char array and thesize?
Code written so far:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#define FILE_NAME "numbers.txt"
int main(int argc, char *argv[])
{
char numbers[MAX];
FILE* pFile;
if(argc != 2)
{
printf("usage:%s large_small_median", argv[0]);
}
else
{
pFile= fopen(argv[1], "r");
if(pFile== 0)
{
printf("Erroropening file ");
}
else
{
intx;
while((x = fgetc(pFile)) != EOF)
{
printf("%c",x);
}
}
while(fgets (numbers, MAX, pFile) != NULL )
{
printf("%s", numbers);
}
fclose(pFile);
}
printf(" ");
getchar();
return 0;
}
void quicksort(int a[], int low, int high)
{
int middle;
if(low >= high)
return;
middle = split(a, low,high);
quicksort(a, low,middle-1);
quicksort(a, middle+1,high);
}
int split(int a[], int low, int high)
{
int part_element = a[low];
for(; ;)
{
while(low< high && part_element <= a[high])
high--;
if(low>= high)
break;
a[low++]= a[high];
while(low< high && a[low] <= part_element)
low++;
if(low>= high)
break;
a[high--]= a[low];
}
a[high] = part_element;
return high; } I am writing a C program that reads integersfrom a text file whose name is given as a command-line argument.The program will display the largest, smallest, and median integerfromthe file.
I have some partial code written to open the file and read thefile, plus I already have the sorting function ready to peice intothe program. I do need help sorting out the "file name given as acommand line argument"
Also can I just call my sort fuction like normal in main(), callingthe function and sending the name of the char array and thesize?
Code written so far:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#define FILE_NAME "numbers.txt"
int main(int argc, char *argv[])
{
char numbers[MAX];
FILE* pFile;
if(argc != 2)
{
printf("usage:%s large_small_median", argv[0]);
}
else
{
pFile= fopen(argv[1], "r");
if(pFile== 0)
{
printf("Erroropening file ");
}
else
{
intx;
while((x = fgetc(pFile)) != EOF)
{
printf("%c",x);
}
}
while(fgets (numbers, MAX, pFile) != NULL )
{
printf("%s", numbers);
}
fclose(pFile);
}
printf(" ");
getchar();
return 0;
}
void quicksort(int a[], int low, int high)
{
int middle;
if(low >= high)
return;
middle = split(a, low,high);
quicksort(a, low,middle-1);
quicksort(a, middle+1,high);
}
int split(int a[], int low, int high)
{
int part_element = a[low];
for(; ;)
{
while(low< high && part_element <= a[high])
high--;
if(low>= high)
break;
a[low++]= a[high];
while(low< high && a[low] <= part_element)
low++;
if(low>= high)
break;
a[high--]= a[low];
}
a[high] = part_element;
return high; }