sorting/ins_sort.c
/**
 * read a set of numbers from a text file, and store them in an array. 
 * @param fname: the name of the file to read from
 * @param array: a pointer to an array structure to populate with data. 
 */
read a set of numbers from a text file, and store them in an array.
@param fname: the name of the file to read from
@param array: a pointer to an array structure to populate with data.
read a set of numbers from a text file and store them in an array . @param fname : the name of the file to read from @param array : a pointer to an array structure to populate with data .
int int_array_read(const char* fname, int_array_t *array) {
    FILE *file = fopen(fname, "r");
    if (file == NULL) {
	return -1;
    }
    fseek(file, 0, SEEK_END);
    int len = ftell(file);
    fseek(file, 0L, SEEK_SET);
    int *tmp = (int*)malloc(len * sizeof(int));
    int n = 0;
    while(fscanf(file, "%i", &tmp[n]) == 1) {
	n++;
    }
    fclose(file);
    array->data = (int*)realloc(tmp, n * sizeof(int));
    array->length = n;
    return 0;
}
int int_array_read ( const char * fname , int_array_t * array ) { FILE * file = fopen ( fname , " " ) ; if ( file == NULL ) { return -1 ; } fseek ( file , 0 , SEEK_END ) ; int len = ftell ( file ) ; fseek ( file , 0L , SEEK_SET ) ; int * tmp = ( int * ) malloc ( len * sizeof ( int ) ) ; int n = 0 ; while ( fscanf ( file , " " , & tmp [ n ] ) == 1 ) { n ++ ; } fclose ( file ) ; array -> data = ( int * ) realloc ( tmp , n * sizeof ( int ) ) ; array -> length = n ; return 0 ; }
read a set of numbers from a text file, and store them in an array.
read a set of numbers from a text file and store them in an array .
// rewind the file // temp storage for the array values, can be at most // the number of bytes in the file. Yes, this is an overestimate // number of numbers read