You do not need nor may you use arrays for this program Writ

You do not need nor may you use arrays for this program. Write a program that merges the numbers in two files and writes the results to a third file. The program reads input from two different files and writes the output to a third file. Each input file contains a list of integers in ascending order, that is, they start small and get bigger as they go. After the program is executed, the output file contains the numbers from the two input files in one longer list, also in ascending order. Your program should define a function to merge the file with three arguments: one for each of the two input FILE pointers and one for the output FILE pointer. The input files should be named numbers1.txt and numbers2.txt, and the output file should be named output.txt. Merging the files will work by trying to read one number from each file. If you have two numbers, compare them and put the smaller one in the output file. Then try to get a new number from the file that you read from to output the last number. If at any point you only have a number from one file, because the other file is empty, then you can simply read and output the numbers one at a time until that file is empty. Some test cases that you may want to consider include the following: What if one of the files contains no numbers? What if both of the files contain no numbers? What happens if one of the files has many more numbers than the other? There is no screen output for your program.

Solution

one.txt

1
2
3
9
12
50
100

two.txt

4
7
11
15
19

main.c

#include <stdio.h>


int io_example(char input1[], char input2[], char output[]){

   FILE *ifp1, *ifp2, *ofp;
   char *imode = \"r\"; // open for reading
   char *omode = \"w\"; // open for writing
   int num = 0;

   // open files
   //
   ifp1 = fopen(input1, imode);
   if (ifp1 == NULL){
       printf(\"Can\'t open file \'%s\': NULL\ \", input1);
       return 0;
   }

   ifp2 = fopen(input2, imode);
   if (ifp2 == NULL){
       printf(\"Can\'t open file \'%s\': NULL\ \", input2);
       return 0;
   }
      
   ofp = fopen(output, omode);
   if (ofp == NULL){
       printf(\"Can\'t open file \'%s\': NULL\ \", output);
       return 0;
   }
  

   while (fscanf(ifp1, \"%d\", &num) == 2){
       fprintf(ofp, \"%d \", num);
   }
  

   return 0;
}


void desc(){
   printf(\"---------------------------------------------------------------\ \");
   printf(\"This program will take two files, and merge them into a third \ \");
   printf(\"sorted numerically. It will accept three file names as command-line\ \");
   printf(\"arguments, or will seek user input of filenames. The first two \ \");
   printf(\"filenames are input files, and the third is the output file. \ \");
   printf(\"---------------------------------------------------------------\ \");
}

int user_input(){

}

int main(int argc, char *argv[]){
   desc();

   int cont = 1;
   int max_args = 4;
   char *input1, *input2, *output;
  
   // collect arguments if 3 or more are given
   if (argc >= 2){
       if (argc > 4)
           printf(\"Too many arguments. argv[1], argv[2] and argv[3] will be used.\ \");
       input1 = argv[1];
       input2 = argv[2];
       output = argv[3];
   }

   // loop the program
   do{
       // [TODO] collect user input if less than 3 arguments are given
       if (argc < 4){
           printf(\"Not enough arguments. Exiting.\ \");
           return 0;
          
       //   printf(\"Please give an input filename:\ > \");
       //   getline(&input1, 60, stdin);
       //   input1 = user_input();  
       //   printf(\"Please give an input filename:\ > \");
       //   getline(input2, 60, stdin);
       //   input2 = user_input();  
       //   printf(\"Please give an output filename:\ > \");
       //   fgets(&output, 60, stdin);
       //   output = user_input();  
       }
  
       argc = 1;
       if (cont == 0){
           return 0;
           printf(\"\ \ \");
       }
      
       cont = io_example(input1, input2, output);
   } while (cont != 0);

   printf(\"\ \ \");
   return 0;
}

 You do not need nor may you use arrays for this program. Write a program that merges the numbers in two files and writes the results to a third file. The progr
 You do not need nor may you use arrays for this program. Write a program that merges the numbers in two files and writes the results to a third file. The progr
 You do not need nor may you use arrays for this program. Write a program that merges the numbers in two files and writes the results to a third file. The progr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site