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.
Thanks in advance

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(;;){

    Len = fread((char *)Input,1,(size_t)MAX,filein);

    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  
 
 [ 1 post ] 

 Relevant Pages 

1. pls help to fix the code!

2. Help on my C code, dont understand how to fix my compiler errors (PLEASE HELP)

3. expert advice needed pls, pls, pls

4. Receive error on compiling single link list codes--pls help

5. TreeView Tooltip code: pls help with the error

6. Help with fix to tab delimted conversion code

7. Can someone help me fix this code?

8. Please help fix code.

9. please help me fix my code

10. Pls, how to change code in VC?

11. Fixed-size arrays as event args in managed code

12. How can I fix these Error Messages (code below)

 

 
Powered by phpBB® Forum Software