Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //void compare_size(int* a, int* b);
  4.  
  5. void scan_file(FILE* fp1, FILE* fp2, FILE* fp3);
  6.  
  7. int main(int argc, char* argv[]) {
  8.     FILE* fp1;
  9.     FILE* fp2;
  10.     FILE* fp3; // eventually
  11.  
  12.     //first bookend
  13.     fp1 = fopen("numbers1.txt", "r");//creates numbers1.txt
  14.     fp2 = fopen("numbers2.txt", "r");//creates numbers2.txt
  15.     fp3 = fopen("output.txt", "w");//creates number3.txt
  16.  
  17.     //checks if file1 opened, if not, exit
  18.     if (fp1 == NULL) {
  19.         printf("Failed to open file 1");
  20.         exit(0);//exits with error
  21.     }
  22.  
  23.     //checks if file 2 opened
  24.     if (fp2 == NULL) {
  25.         printf("Failed to open file 2");
  26.         exit(0);//exits with error
  27.     }
  28.  
  29.     if (fp3 == NULL) {
  30.         printf("Failed to open file 3");
  31.         exit(0);
  32.     }
  33.  
  34.     scan_file(fp1, fp2, fp3);
  35.  
  36.     //second bookend
  37.     fclose(fp1); fp1 = NULL;
  38.     fclose(fp2); fp2 = NULL;
  39.     fclose(fp3); fp3 = NULL;
  40.  
  41.     return 0;
  42. }
  43.  
  44. void scan_file(FILE* fp1, FILE* fp2, FILE* fp3) {
  45.     int x, y, smaller,larger;
  46.     int noc1, noc2;
  47.  
  48.     noc1 = fscanf(fp1, "%d", &x);
  49.     noc2 = fscanf(fp2, "%d", &y);
  50.  
  51.     do {
  52.  
  53.  
  54.             if (noc1 == 1 || noc2 == 1) {
  55.                 if (x < y) {
  56.                     smaller = x;
  57.                     noc1 = fscanf(fp1, "%d", &x);
  58.                   //  larger = y;
  59.                     //  noc2 = fscanf(fp2, "%d", &y);
  60.  
  61.                 }
  62.  
  63.                 else {
  64.                     smaller = y;
  65.                     noc2 = fscanf(fp2, "%d", &y);
  66.                     //larger = x;
  67.                   // noc1 = fscanf(fp1, "%d", &x);
  68.  
  69.                 }
  70.  
  71.             fprintf(fp3, "%d\n", smaller);
  72.           //  fprintf(fp3, "%d\n", larger );
  73.  
  74.             while (noc1 == 1) {
  75.  
  76.                 fprintf(fp3, "%d\n", x);
  77.                 noc1 = fscanf(fp1, "%d", &x);
  78.             }
  79.  
  80.             while (noc2 == 1) {
  81.  
  82.                 fprintf(fp3, "%d\n", y);
  83.                 noc2 = fscanf(fp2, "%d", &y);
  84.  
  85.             }
  86.  
  87.  
  88.         }
  89.  
  90.     } while (noc1 == 1 || noc2 == 1);
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement