Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define INCORRECT_INPUT (-2)
- #define NO_FILE (-1)
- #define SUCCESS 0
- #define INCORRECT_NUMB (-3)
- #define N_MAX (100)
- #define APPLICATION_ERR (-4)
- #define EMPTY_FILE (-5)
- #include "printArray.h"
- #include "sort.h"
- int readArray(FILE *f, int *a)
- {
- int N; // количество чисел
- int numb;
- int i = 0;
- if ( fscanf(f, "%d", &N) != 1)
- {
- printf("Incorrect number of numbers");
- return INCORRECT_NUMB;
- }
- while (!feof(f))
- {
- fscanf(f, "%d", &numb);
- a[i] = numb;
- i++;
- }
- if ( (i--) != N)
- {
- printf("The number of numbers doesn't match application");
- return APPLICATION_ERR;
- }
- return N;
- }
- int main(int argc, char** argv)
- {
- if (argc != 2)
- {
- printf("Incorrect input");
- return INCORRECT_INPUT;
- }
- FILE *f;
- char *filename = argv[1];
- int a[N_MAX] = {0};
- char tmp;
- f = fopen(filename, "r");
- if (f == NULL)
- {
- printf("This file doesn't exist");
- return NO_FILE;
- }
- if (fscanf(f,"%c",&tmp) != 1)
- {
- printf("The file is empty.\n");
- return EMPTY_FILE;
- }
- fseek(f,0,SEEK_SET);
- int N;
- N = readArray(f, a);
- if (N == APPLICATION_ERR)
- return APPLICATION_ERR;
- printArray(a, &N);
- printf("\n");
- sort(a, &N);
- printArray(a, &N);
- fclose(f);
- return SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement