读取大文本文件并在C程序中按字母顺序快速排序

时间:2016-04-24 14:43:28

标签: c

我可以成功地显示文字的字样。 但我不能按字母顺序排序。

如何将文字插入字符 myArray =(char )malloc(size);

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static int compare (const void * a, const void * b);

int main(int argc, char *argv[])
{
    FILE *fp;
    char ch;
    int size = 0;

    fp = fopen("data.txt", "r");
    if (fp == NULL)
        printf("\nFile unable to open ");
    else 
        printf("\nFile opened ");
    fseek(fp, 0, 2);    /* file pointer at the end of file */
    size = ftell(fp);   /* take a position of file pointer un size variable */
    //char *myArray =  (char*)malloc(size * sizeof *myArray);
    char *myArray = (char*)malloc(size);


    static const char filename[] = "data.txt";
    FILE *file = fopen(filename, "r");
    if ( file != NULL ){
        int ch, word = 0, index= 0,index2 = 0;
        while ( (ch = fgetc(file)) != EOF ){
            if ( isspace(ch) || ispunct(ch) ){
                if ( word ){
                    word = 0;
                    myArray[index++] = '\n';
                   //putchar('\n');
                }
            }else{
                word = 1;
                //putchar(ch);
                myArray[index++] = ch;
                index2++;
            }
        }
        printf("%s", myArray);
        fclose(file);
        int i;
        for(i = 0;i < sizeof(myArray);i++){
            putchar(myArray[i]);
        }

        //qsort (array, 2, sizeof (const char *), compare);
        //for (int i = 0; i < 2; i++) {
        //    printf ("%d: %s.\n", i, array[i]);
        //}
    }
}

static int compare (const void * a, const void * b)
{
    return strcmp (*(const char **) a, *(const char **) b);
}

1 个答案:

答案 0 :(得分:0)

qsort()调用需要一个指向字符串的指针数组。所以你需要以这种方式组织你的话。例如:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char myArray[] = "This\nis\na\nsample\ntext\nstring\nas\nthough\nfrom\nyour\nfile";
char **myWords;

//
// Compare two strings case insensitive
//
static int compare (const void * a, const void * b)
{
    char *aStr, *bStr;
    int i, retVal;

    if ( !( aStr = strdup( *(const char **) a ) ) )
       return 1;

    if ( !( bStr = strdup( *(const char **) b ) ) ) {
       free( aStr );
       return -1;
    }

    for ( i=0; i<strlen( aStr ); i++ ) 
       aStr[i] = (char)tolower( (int)aStr[i] );

    for ( i=0; i<strlen( bStr ); i++ ) 
       bStr[i] = (char)tolower( (int)bStr[i] );

    retVal = strcmp( aStr, bStr );

    free( aStr );
    free( bStr );

    return retVal;
}

int main ()
{
    char *curWord, *nextWord;
    long wordCount;
    int i;

    //
    // myArray contains an array of newline delimited words as
    // you have in your code after reading from the file
    //
    for ( i=0; i<strlen(myArray); i++ ) 
       putchar( myArray[i] );

    //
    // If myArray is empty then no need to sort
    //
    if ( strlen( myArray ) == 0 ) {
       printf( "Nothing to sort\n" );
       return 0;
    }

    //
    // Count how many newlines are in myArray - assume at least one
    //
    for ( wordCount=1, nextWord=myArray;
          ( nextWord = strchr( nextWord, (int)'\n' ) );
          wordCount++, nextWord++ );

    //
    // If only one word then no need to sort
    //
    if ( wordCount == 1 ) {
        printf ("\n0: %s.\n", myArray );
        return 0;
    }

    // 
    // Allocate enough space to hold a new copy of each word 
    // 
    if ( !( myWords = calloc( wordCount, sizeof(char *) ) ) ) 
       return 1; 

    //
    // Load words from myArray into myWords 
    //
    curWord = nextWord = myArray; 
    i = 0;
    while ( ( nextWord = strchr( nextWord, (int)'\n' ) ) ) {
        *nextWord++ = '\0';
        myWords[i] = strdup( curWord );
        if ( !myWords[i] ) {
           printf( "\nFile too big for memory\n" );
           return 1;
        }
        i++;
        curWord = nextWord;
    }
    myWords[i] = curWord;

    //
    // Sort the array of words in myWords
    //
    qsort( myWords, wordCount, sizeof(char *), compare );

    //
    // Dump out sorted array
    //
    printf( "\n" );
    for( i = 0; i < wordCount; i++ ) 
        printf( "%d: %s.\n", i, myWords[i] );

    //
    // Free your memory here - but since exiting main() and 
    // program is ending no need to worry about it.
    //

    return 0;
}