Advertisement
Bisus

Untitled

Oct 6th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     FILE *f;
  7.     int *a, *b;
  8.     int i, n, m;
  9.  
  10.     if((f = fopen("input.txt", "r"))==NULL)
  11.     {
  12.         printf("Open file error!\n");
  13.         return 1;
  14.     }
  15.  
  16.     if(fscanf(f, "%d", &n)<=0)// Считываю кол-во элементов в 1ом массиве
  17.     {
  18.         printf("Input from file error!\n");
  19.         return 2;
  20.     }
  21.  
  22.     a = (int*)malloc(n*sizeof(int));
  23.     for(i = 0; i<n; i++)
  24.     {
  25.         if(fscanf(f, "%d", &a[i])<=0)
  26.         {
  27.             printf("Input from file error!\n");
  28.             free(a);
  29.             return 2;
  30.         }
  31.     }
  32.  
  33.     if(fscanf(f, "%d", &m)<=0)// Считываю кол-во элементов в 2ом массиве
  34.     {
  35.         printf("Input from file error!\n");
  36.         free(a);
  37.         return 2;
  38.     }
  39.  
  40.     b = (int*)malloc(m*sizeof(int));
  41.     for(i = 0; i<m; i++)
  42.     {
  43.         if(fscanf(f, "%d", &b[i])<=0)
  44.         {
  45.             printf("Input from file error!\n");
  46.             free(a);
  47.             free(b);
  48.             return 2;
  49.         }
  50.     }
  51.     // Массивы считаны
  52.  
  53.     free(a);
  54.     free(b);
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement