Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #define INCORRECT_INPUT (-2)
  3. #define NO_FILE (-1)
  4. #define SUCCESS 0
  5. #define INCORRECT_NUMB (-3)
  6. #define N_MAX (100)
  7. #define APPLICATION_ERR (-4)
  8. #define EMPTY_FILE (-5)
  9.  
  10. #include "printArray.h"
  11. #include "sort.h"
  12.  
  13. int readArray(FILE *f, int *a)
  14. {
  15.     int N; // количество чисел
  16.     int numb;
  17.     int i = 0;
  18.    
  19.     if ( fscanf(f, "%d", &N) != 1)
  20.     {
  21.         printf("Incorrect number of numbers");
  22.         return INCORRECT_NUMB;
  23.     }
  24.  
  25.     while (!feof(f))
  26.     {
  27.         fscanf(f, "%d", &numb);
  28.         a[i] = numb;
  29.         i++;
  30.     }
  31.    
  32.     if ( (i--) != N)
  33.     {
  34.         printf("The number of numbers doesn't match application");
  35.         return APPLICATION_ERR;
  36.     }
  37.    
  38.     return N;
  39. }
  40.  
  41. int main(int argc, char** argv)
  42. {
  43.     if (argc != 2)
  44.     {
  45.         printf("Incorrect input");
  46.         return INCORRECT_INPUT;
  47.     }
  48.    
  49.     FILE *f;
  50.     char *filename = argv[1];
  51.     int a[N_MAX] = {0};
  52.     char tmp;
  53.    
  54.    
  55.     f = fopen(filename, "r");
  56.    
  57.     if (f == NULL)
  58.     {
  59.         printf("This file doesn't exist");
  60.         return NO_FILE;
  61.     }
  62.     if (fscanf(f,"%c",&tmp) != 1)
  63.     {
  64.         printf("The file is empty.\n");
  65.         return EMPTY_FILE;
  66.     }
  67.    
  68.     fseek(f,0,SEEK_SET);
  69.    
  70.     int N;
  71.     N = readArray(f, a);
  72.     if (N == APPLICATION_ERR)
  73.         return APPLICATION_ERR;
  74.     printArray(a, &N);
  75.     printf("\n");
  76.     sort(a, &N);
  77.     printArray(a, &N);
  78.     fclose(f);
  79.     return SUCCESS;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement