sorting - fread reading garbage value from stdin -



sorting - fread reading garbage value from stdin -

the next code sorting numbers based on quicksort in c. optimize speed, scanf() has been replaced fread(). on printing sorted list, garbage values come out. exact problem statement on :http://www.codechef.com/problems/tsort

#define size 32768 #include <stdio.h> #include <stdlib.h> int compare(const void *a, const void *b) { homecoming (*(unsigned long *)a-*(unsigned long *)b); } int main() { char buffer[size]; register int readbytes=0; unsigned long buffercounter=0, bufferedinput=0; unsigned long * array, t, i; scanf("%lu", &t); fflush(stdin); array=(unsigned long *) malloc(sizeof(long)*t); i=-1; while(readbytes=fread(buffer, sizeof(char), size, stdin)>0) { for(buffercounter=0; buffercounter<readbytes; buffercounter++) { if(buffer[buffercounter]=='\n' || buffer[buffercounter]==' ') { array[++i]=bufferedinput; bufferedinput=0; } else bufferedinput=bufferedinput*10+(buffer[buffercounter]-'0'); } } qsort(array, t, sizeof(long), compare); for(i=0; i<t; i++) { printf("%lu\n", array[i]); } homecoming 0; }

the code compiled in codeblocks , input piped file.

the input file :

5 5 3 6 7 1

the output obtained is:

5050160 5056368 1465662019 1868852841 1935438711

what problem code?

sorting io fread qsort garbage

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -