output a sorted file in C using qsort -
to read strings file , print sorted output using qsort. write this:
int main() { int n=0; int size=1; file *fp = fopen(args[0],"r"); int c; char* inputfile; inputfile = char* malloc(size); if(fp==0){ fprintf(stderr, "cannot open file!\n"); homecoming -1; else{ do{ c = fgetc(fp); if(size==1){ inputfile[n]=c; } else{ inputfile = char* realloc(inputfile, size+1); inputfile[n]=c; } n++; size++; }while(c!=eof); qsort(inputfile, 1, size, compare);//i have implement compare function correctly n=0; while(n<size){ while(input[n]!='\0'){ printf ("%d ",inputfile[n]); n++; } n++; } homecoming 0; }
so, if input file '\0vaaa\n\0ba\0\nabc', programme should output print:
abc ba vaaa
however, code isn't working @ all. have check compare method homecoming right result. additionally, wonder if implement malloc-realloc correctly? thx
you want print strings, replace:
printf ("%d ",inputfile[n]);
with
printf ("%s ",inputfile[n]);
but may have other problems in code..
c
No comments:
Post a Comment