#include #include #include /*Input: Integer array and its size * Output: None * Return: None * Notes: Function to sort the integers in the array */ void sort(int arr[], int * size) { /*initializes local variables needed to run bubble sort*/ int i; int j; int tmp; /*bubble sort implementation*/ for(i=0; i<(*size-1); i++) { for(j=0; j<(*size-1); j++) { if (arr[j]>arr[j+1]) { tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } } /*Input: Name of input and output files as command line argument * Output: Writes sorted array to specified output file * Return: Type = int, value = 0 * Notes: main method which reads integers from given input file, calls sort * function to sort the integers, then writes the sorted ints to given * output file */ int main( int argc, char ** argv ) { /*declares all variables*/ int i; int tmp; /*store the position in char array argv of input and output files * respectively */ int input; int output; /*checki and checko facilitate verifying valid input and output file * flags were entered */ int checki=0; int checko=0; /*tracks number of ints read from file*/ int num_items=0; /*grow rate for array*/ int grow=10; /*used to store return value for fread*/ size_t size; /*array declaration and memory allocation*/ int * arr = (int *)malloc( sizeof( int ) * grow); /*declares file pointer fptr*/ FILE * fptr; /*for loop to check for flags declaring input and output files at * command line, when the appropriate flag is identified, the array * position of the input and output files is stored in the variables * input and output respectively. checki/checko incrememnt to signal * that a valid input/output file was entered when running program. */ for ( i = 1 ; i < argc ; ++i ) { if (strcmp(argv[i], "-i")==0) { input = i + 1; checki++; } if (strcmp(argv[i], "-o")==0) { output = i + 1; checko++; } } /*if either the input or the output files or flags were not entered, * error message prints to screen, program exits. */ if (checki==0 || checko==0) { printf("enter valid input and output files!\n"); exit( 1 ); } /*open input file with read access*/ fptr = fopen ( argv[input], "r" ); /*resets i value to zero*/ i = 0; /*this loop reads ints from file 1 at a time, stores value in array * buf, then grows array arr, and stores value in buf in arr[i] where * i is the current loop iteration. also tracks number of values * stored in the array. Loop runs as long as fread is successful in * reading one int */ do { size = fread ( &tmp , sizeof( int ), 1, fptr); grow+=10; arr=(int *)realloc(arr, sizeof(int)*grow); arr[i]=tmp; /*if/block to ensure that num_items does not increment on final * iteration of do/while loop */ if (size == 1) { num_items++; } ++i; } while( size == 1 ); /*calls sort function to sort array arr*/ sort(arr, &num_items); /*closes input file*/ fclose( fptr ); /*opens output file with write access*/ fptr = fopen ( argv[output], "w" ); /*writes integers stored array arr to output file*/ fwrite ( arr, sizeof( int ), num_items, fptr ); /*closes output file*/ fclose( fptr ); /*frees memory allocated to arr*/ free( arr ); return 0; }