2. Name this program split.c-The program takes two command line arguments, the n
ID: 3589513 • Letter: 2
Question
2. Name this program split.c-The program takes two command line arguments, the name of an input file that contains a set of integers and an integer. If the input file does not exist then print an error message and exit the program. If the file exists, then it is guaranteed to only contain valid integers. . Your program should split this input file into two separate files, one (named less.txt) containing integers that are less than the threshold value specified on the command line, and one (named more.txt) containing the integers that are greater than the threshold value. .Both your output files, less.txt and more.txt, should print one number per line. Note that you do not write any occurrences of the threshold value itself to either file. /a.out myData 100 ./a.out noSuchFile0 The file specified, noSuchFile, does not existExplanation / Answer
#include <stdio.h>
#include <math.h>
#include<string.h>
#include<stdlib.h>
FILE *fp,*fpw,*fpw1;
void printArray(int arr[], int n,int threshold,int x)
{
int i;
for (i=0; i < n; i++)
{
if(x==0)
{
if(arr[i]<threshold){
fprintf(fpw,"%d", arr[i]);
fprintf(fpw," ");
}
}
else if(x==1)
{
if(arr[i]>threshold){
fprintf(fpw,"%d", arr[i]);
fprintf(fpw," ");
}
}
}
}
int main(int argc, char* argv[])
{
int arr[1000] = {0};
int threshold=0;
int arr_size =0;
int data;
char file1[20];
strcpy(file1,argv[1]);
threshold=atoi(argv[2]);
fp = fopen(file1,"r+");
if (fp == NULL) // if file not opened return error
{
printf("The file specified %s does not exist",file1);
return -1;
}
else
{
fscanf (fp, "%d", &data);
arr[arr_size]=data;
arr_size++;
while (!feof (fp))
{
fscanf (fp, "%d", &data);
arr[arr_size]=data;
arr_size++;
}
}
fclose(fp);
char file2[20];
strcpy(file2,"less.txt");
fpw = fopen(file2,"w");
if (fpw == NULL) // if file not opened return error
{
perror("Unable to create file");
return -1;
}
printArray(arr, arr_size,threshold,0);
fclose(fpw);
char file3[20];
strcpy(file3,"more.txt");
fpw1 = fopen(file3,"w");
if (fpw1 == NULL) // if file not opened return error
{
perror("Unable to create file");
return -1;
}
printArray(arr, arr_size,threshold,1);
fclose(fpw1);
fpw1=NULL;
fpw=NULL;
fp=NULL;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.