Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdarg.h>
- void error(int error_code); //Function with array which contains errors
- void open_files_to_read(int quantity,...); //Function which will open files are needed
- void close_files(int quantity, ...); //Function which will close all files
- short int load_array(long int **array,FILE *output); //To load array from file and returns it's quantity of elements
- int main(int argc, char *argv[])
- {
- FILE *input1 = NULL, *input2 = NULL, *output = NULL; //file's pointers
- long int*array1 = NULL , *array2 = NULL; //variant with the local variables
- int quantity = 2;
- open_files_to_read(quantity , &input1, "fill_first_array.txt", &input2, "fill_second_array.txt"); //Open two files to read
- load_array(&array1, input1); //To load the first array from file
- if (!(output = fopen(argv[3], "w")))
- {
- printf("Here is the error while opening output file &s !" , argv[3]);
- exit(3);
- }
- close_files(3,&input1, &input2, &output); //Close all files
- return 0;
- }
- short int load_array(long int **array,FILE* input)
- {
- int counter = 0, decimal = 0 , numbers = 0 ;
- char current_symbol, *temporary_buffer = NULL;
- array = (long int**)malloc(sizeof(long int*)); //To allocate memory for the pointer which points on the array
- while (!feof(input))
- {
- current_symbol = fgetc(input);
- if ((current_symbol != ' ') && (current_symbol != EOF) && (current_symbol != '\n'))
- {
- temporary_buffer = (char*)realloc(temporary_buffer, ++counter * sizeof(char));
- temporary_buffer[counter-1] = current_symbol;
- }
- if ((current_symbol == ' ') || (current_symbol == '\n'))
- {
- numbers++;//To estimate how many numbers we got
- decimal = 0;
- int digits = 0;
- for (int coun = 0 ; coun<counter;coun++) //To use gorner to convert string into integer
- {
- decimal = decimal * 10 + (isdigit(temporary_buffer[coun]) ? (temporary_buffer[coun] - '0') : (temporary_buffer[coun] - 'A' + 10));
- digits++;
- }
- *array = (long int*)realloc(*array , numbers * sizeof(long int)); //To allocate memory for the array
- (*array)[numbers - 1] = decimal; //To fill our array by number
- free(temporary_buffer); //Reset buffer
- temporary_buffer = NULL; //Reset it's adress
- counter = 0; //Reset counter of digits
- }
- }
- return numbers;
- }
- void open_files_to_read(int quantity,... )
- {
- va_list argument_pointer;
- va_start(argument_pointer, quantity);
- FILE** f;
- char* fName;
- while (quantity--)
- {
- f = va_arg(argument_pointer, FILE**);
- fName = va_arg(argument_pointer, char*);
- //if (! ( *(va_arg(argument_pointer, FILE**)) = fopen(va_arg(argument_pointer, char*), "r") ) ) ;
- if (!(*f = fopen(fName, "r")))
- {
- error(1);
- }
- }
- va_end(argument_pointer);
- return;
- }
- void close_files(int quantity,...)
- {
- int counter = 0;
- va_list argument_pointer;
- va_start(argument_pointer, quantity);
- while (counter++ < quantity)
- {
- fclose(*(va_arg(argument_pointer, FILE**)));
- }
- va_end(argument_pointer);
- return;
- }
- void error(int error_code)
- {
- static char * errors_array[] =
- {
- "Here is the error while opening input file to read!" //The '0' error
- ,"Here is the error while opening output file to write!" //The 1st
- ,
- };
- printf("Is is error number #%d,\n%s\n", error_code, *errors_array[error_code - 1]);
- exit(error_code);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement