to read strings from a file and print the sorted output using qsort. So I write something like 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");
return -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 the compare function correctly
n=0;
while(n<size){
while(input[n]!='\0'){
printf ("%d ",inputFile[n]);
n++;
}
n++;
}
return 0;
}
So, if the input file is ‘\0vaaa\n\0ba\0\nabc’, the program should output print:
abc
ba
vaaa
However, my code isn’t working at all. I have check that the compare method return the correct result. Additionally, I just wonder if I implement the malloc-realloc correctly? Thx
You want to print strings, so replace:
with
But you may have other problems in your code..