Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void read_numbers(FILE* fp1, FILE* fp2, FILE* fp3);
  5.  
  6. void swap(int* a, int* b);
  7.  
  8. int main(void)
  9. {
  10.     // read all file at once
  11.    FILE *fp1 = fopen("numbers1.txt", "r");
  12.    FILE *fp2 = fopen("numbers2.txt", "r");
  13.  
  14.    // Open file to store the result
  15.    FILE *fp3 = fopen("output.txt", "w");
  16.    //char c;
  17.  
  18.    if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
  19.    {
  20.          printf("Could not open files");
  21.          exit(1);
  22.    }
  23.  
  24.    //function to read file 1 and 2, compare it, and output to fp3.
  25.    read_numbers(fp1,fp2,fp3);
  26.  
  27.    fclose(fp1); fp1 = NULL;
  28.    fclose(fp2); fp2 = NULL;
  29.    fclose(fp3); fp3 = NULL;
  30.  
  31.     return 0;
  32. }
  33.  
  34. void read_numbers(FILE* fp1, FILE* fp2, FILE* fp3)
  35. {
  36.     int number1, number2;
  37.     int noc1;
  38.     int noc2;
  39.     int smaller;
  40.  
  41.     noc1 = fscanf(fp1, "%d", &number1);
  42.     noc2 = fscanf(fp2, "%d", &number2);
  43.     while (noc1 == 1 && noc2 == 1)
  44.     {
  45.         if(number1 <= number2)
  46.         {
  47.           smaller = number1;
  48.           noc1 = fscanf(fp1, "%d", &number1);
  49.         }
  50.         else
  51.         {
  52.           smaller = number2;
  53.           noc2 = fscanf(fp2, "%d", &number2);
  54.         }
  55.         fprintf(fp3, "%d\n",smaller);
  56.     }
  57.     while(noc1 == 1) // If file 1 has stuff
  58.     {
  59.       fprintf(fp3, "%d\n",number1 );
  60.       noc1 = fscanf(fp1, "%d", &number1); // read it and dump it into fp3
  61.     }
  62.  
  63.     while (noc2 == 1)
  64.     {
  65.       //printf("HI %d\n", number2);
  66.       fprintf(fp3, "%d\n",number2);
  67.       noc2 = fscanf(fp2, "%d", &number2); // read it and dump it into fp3
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement