Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8.  
  9. void error(int error_code); //Function with array which contains errors
  10. void open_files_to_read(int quantity,...); //Function which will open files are needed
  11. void close_files(int quantity, ...); //Function which will close all files
  12.  
  13. short int load_array(long int **array,FILE *output); //To load array from file and returns it's quantity of elements
  14.  
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     FILE *input1 = NULL, *input2 = NULL, *output = NULL;  //file's pointers
  19.  
  20.     long int*array1 = NULL , *array2 = NULL; //variant with the local variables
  21.  
  22.     int quantity = 2;
  23.  
  24.     open_files_to_read(quantity , &input1, "fill_first_array.txt", &input2, "fill_second_array.txt"); //Open two files to read
  25.  
  26.     load_array(&array1, input1); //To load the first array from file
  27.  
  28.     if (!(output = fopen(argv[3], "w")))
  29.     {
  30.         printf("Here is the error while opening output file &s !" , argv[3]);
  31.         exit(3);
  32.     }
  33.  
  34.  
  35.  
  36.     close_files(3,&input1, &input2, &output); //Close all files
  37.  
  38.     return 0;
  39. }
  40.  
  41.  
  42.  
  43. short int load_array(long int **array,FILE* input)
  44. {
  45.     int counter = 0, decimal = 0 , numbers = 0 ;
  46.     char current_symbol, *temporary_buffer = NULL;
  47.  
  48.     array = (long int**)malloc(sizeof(long int*)); //To allocate memory for the pointer which points on the array
  49.  
  50.     while (!feof(input))
  51.          {
  52.            
  53.             current_symbol = fgetc(input);
  54.             if ((current_symbol != ' ') && (current_symbol != EOF) && (current_symbol != '\n'))
  55.                {
  56.                    temporary_buffer = (char*)realloc(temporary_buffer, ++counter * sizeof(char));
  57.                    temporary_buffer[counter-1] = current_symbol;
  58.                }
  59.             if ((current_symbol == ' ')  || (current_symbol == '\n'))
  60.             {
  61.                 numbers++;//To estimate how many numbers we got
  62.                 decimal = 0;
  63.  
  64.                 int digits = 0;
  65.  
  66.                 for (int coun = 0  ; coun<counter;coun++) //To use gorner to convert string into integer
  67.                      {
  68.                          decimal = decimal * 10 + (isdigit(temporary_buffer[coun]) ? (temporary_buffer[coun] - '0') : (temporary_buffer[coun] - 'A' + 10));  
  69.                          digits++;
  70.                      }
  71.                 *array = (long int*)realloc(*array , numbers * sizeof(long int)); //To allocate memory for the array
  72.                 (*array)[numbers - 1] = decimal; //To fill our array by number
  73.                 free(temporary_buffer); //Reset buffer
  74.                 temporary_buffer = NULL; //Reset it's adress
  75.                 counter = 0; //Reset counter of digits
  76.             }
  77.              
  78.          }
  79.  
  80.  
  81.     return numbers;
  82. }
  83.  
  84.  
  85.  
  86.  
  87. void open_files_to_read(int quantity,... )
  88. {
  89.  
  90.     va_list argument_pointer;
  91.     va_start(argument_pointer, quantity);
  92.     FILE** f;
  93.     char* fName;
  94.     while (quantity--)
  95.     {
  96.         f = va_arg(argument_pointer, FILE**);
  97.         fName = va_arg(argument_pointer, char*);
  98.         //if  (! ( *(va_arg(argument_pointer, FILE**))  = fopen(va_arg(argument_pointer, char*), "r") ) ) ;
  99.         if (!(*f = fopen(fName, "r")))
  100.            {
  101.             error(1);
  102.            }
  103.     }
  104.  
  105.     va_end(argument_pointer);
  106.  
  107.     return;
  108. }
  109.  
  110. void close_files(int quantity,...)
  111. {
  112.     int counter = 0;
  113.  
  114.     va_list argument_pointer;
  115.     va_start(argument_pointer, quantity);
  116.  
  117.     while (counter++ < quantity)
  118.        {
  119.             fclose(*(va_arg(argument_pointer, FILE**)));
  120.        }
  121.  
  122.     va_end(argument_pointer);
  123.  
  124.     return;
  125. }
  126.  
  127. void error(int error_code)
  128. {
  129.  
  130.     static char * errors_array[] =
  131.     {
  132.         "Here is the error while opening input file to read!" //The '0' error
  133.          ,"Here is the error while opening output file to write!" //The 1st
  134.          ,
  135.     };
  136.  
  137.     printf("Is is error number #%d,\n%s\n", error_code, *errors_array[error_code - 1]);
  138.     exit(error_code);
  139.     return;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement