Pls help me to fix the code!
Author Message
Pls help me to fix the code!

Hi,
I would like to perform the algorithm:
Given abbab
abb a
bba b
bab a
bab b
the sort the first 3 characters become
abb a
bab a
bab b
bba b
and write the final stream aabb out to file.
I perform the following code.
But it gives wrong result.
Anyone can help me to fix it? and
and fast method? as I feel it is very
slow when a large file comes in even quick sort.

DO()
{
FILE *filein,*fileout;
char in[15],*p,x,out[15],tmp[MAX],Input[MAX];
char * Array;
char ** Source,*temp;
long int Len, i,n=0;
char Elem[10];

printf("Enter an input file\n");
scanf("%s",in);
printf("Enter an output file\n");
scanf("%s",out);
printf("Enter the N number \n");
scanf("%d", &N);

N=N+1; /* N =3 in my case */

filein=fopen(in,"rb");
fileout=fopen(out,"wb");

setmode(fileno(filein),O_BINARY);
setmode(fileno(fileout),O_BINARY);

for(;;){

if(Len==0) break;
pArray = malloc(nLen*2);
memcpy(Array,Input,Len);
memcpy(Array+Len,Input,Len);
Source = malloc(Len*sizeof(char*));
for(i=0; i<Len; i++)
{
Source[i] = &Array[i];
memset(Elem,0,sizeof(Elem));
memcpy(Elem, Source[i], N);
}

qsort( Source, Len, sizeof(char *), compare);
for(i=0; i<Len; i++)
{
memset(Elem,0,sizeof(Elem));
memcpy(Elem, Source[i], N);
}
for(i=0; i<Len; i++)
{
fputc(*(Source[i]),fileout);
}
} //end for
free(Array);
free(Source);

printf("Press any key ...\n");
getch();
fclose(filein);
fclose(fileout);

Quote:
}

int compare(const void *elem1, const void *elem2 )
{
char *pElem1, *pElem2;
pElem1 = *((char**)elem1)+1;
pElem2 = *((char**)elem2)+1;
count++;
return memcmp( pElem1, pElem2, N - 1 );
Quote:
}

--

Fri, 23 Nov 2001 03:00:00 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages