Question

In: Computer Science

IN C LANGUAGE This program takes two command line arguments: an input filename a threshold Your...

IN C LANGUAGE

This program takes two command line arguments:

  1. an input filename
  2. a threshold

Your program will create two files:

  1. even.txt - contains all integers from the input file that are even and greater than the threshold
  2. odd.txt - contains all integers from the input file that are odd and greater than the threshold
  • The input file will exist and only contain a set of integers. It will always be valid data.
  • Output whitespace will be ignored.
  • Name the files even.txt and odd.txt.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>


int main(int argc, char *argv[]){
  
   // convert command line number to integer
   int threshold = atoi(argv[2]);
  
   char even_filename[] = "even.txt";
   char odd_filename[] = "odd.txt";
  
   FILE* infile = fopen(argv[1],"r");
   FILE* evenfile = fopen(even_filename,"w");
   FILE* oddfile = fopen(odd_filename,"w");
  
   if(infile==NULL || evenfile==NULL || oddfile==NULL){
       printf("Error: Reading Writing to files.");
       return 1;
   }
  
   int val;
   while(fscanf(infile,"%d",&val)!=EOF){
       if(val%2==0 && val > threshold){
           fprintf(evenfile,"%d\n", val);
       }
       else if(val%2==1 && val>threshold){
           fprintf(oddfile,"%d\n", val);
       }
   }
   // close the file handlers
   fclose(infile);
   fclose(evenfile);
   fclose(oddfile);
  
   printf("Files generated successfully.");
  
  
}

==================================================================


Related Solutions

program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a program that takes two command line arguments at the time the program is executed....
Write a program that takes two command line arguments at the time the program is executed. You may assume the user enters only decimal numeric characters. The input must be fully qualified, and the user should be notified of any value out of range for a 23-bit unsigned integer. The first argument is to be considered a data field. This data field is to be is operated upon by a mask defined by the second argument. The program should display...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the...
16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the program will read...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command...
IN C LANGUAGE 16.15 Lab 5: filter Name this program filter.c. The program takes two command line arguments: the name of an input file and the name of an output file. The program should confirm the input and output files can be opened. If a file cannot be opened, print the error message Cannot open file '<filename>' where <filename> is the name of the file and return. fopen() will return 0 if it fails to open a file. Then, the...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments...
write a program ordered.java Order check Write a program Ordered.java that takes four int command-line arguments w, x, y, and z. Define a boolean variable whose value is true if the four values are either in strictly ascending order (w < x < y < z) or strictly descending order (w > x > y > z), and false otherwise. Then, display the boolean variable value. NOTE 1: Do not use if statements on this program. NOTE 2: Assume that...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4 ME 2 finish 2! Output: Consonants: WMfnsh 1) Name your program command_consonants.c. 2) If no command line arguments was provided, your program should print a usage message “Usage: ./a.out input #include<stdio.h> int main() { printf("Input: "); int i = 0; char ch; // array to store consonants char consonant[100]={""}; //do loop to take input do { ch = getchar(); //checks to see if consonants...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that...
Complete Question 1a-c 1a) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 1b) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT