There is input file; unsorted long type numbers.(about 1 million) I want to sort numbers in input files. To allocate the memory of array, I used fseek and ftell. But segmentation fault is occurred. How do i fix the code?
int main( int argc, char *argv[] )
{
long *arr;
FILE *fp = NULL;
FILE *fp2 = NULL;
int i = 0;
long size, count;
fp = fopen( "argv[1]", "r" );
fp2 = fopen( "sorted", "w" );
if ( fp == NULL || fp2 == NULL )
printf( "error \n" );
else
{
fseek( fp, 0, SEEK_END );
size = ftell( fp );
arr = ( long * ) malloc( size );
count = fread( arr, size, 4, fp );
Quicksort( arr, 0, sizeof( arr ) / sizeof( int ) - 1 );
while ( i != ( sizeof( arr ) / sizeof( int ) - 1 ) )
{
int i = 0;
int j;
for ( j = 0; j < 5; j++ )
fwrite( arr, size, 1, fp2 );
}
}
fclose( fp );
free( arr );
fclose( fp2 );
return 0;
}