从文件中读取txt,将其剪切为单词并显示

时间:2016-05-13 10:30:31

标签: c

#pragma warning(disable:4996) 
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>
#include <ctype.h>

static WORDS heap[10000];
int heapSize;

void InitHeap()
{
    heapSize = 0;
    heap[0].words = NULL;
    heap[0].count = -1;
}

void InsertHeap(char* string)
{
    heapSize++;
    strcpy(heap[heapSize].words, string); 

    int now = heapSize;
    while (heap[now / 2].words > string)
    {
        heap[now] = heap[now / 2];
        now /= 2;
    }
    strcpy(heap[now].words, string);
}

int DeleteHeap()
{
    char* minElement, lastElement;
    int child, now;

    strcpy(minElement, heap[1].words);
    strcpy(lastElement, heap[heapSize--].words);

    for (now = 1; now * 2 <= heapSize; now = child)
    {
        child = now * 2;
        if (child != heapSize && heap[child + 1].words < heap[child].words)
        {
            child++;
        }
        if (lastElement > heap[child].words)
        {
            strcpy(heap[now].words, heap[child].words);
        }
        else
        {
            break;
        }
    }
    strcpy(heap[now].words, lastElement);

    return now;
}

typedef struct _WORDS {
    char    words[64];
    int     count;
}WORDS;


char* MakeToken(void)
{
    int i, j;  
    static char delim[256];
    memset(delim, 0x0, 256);

    for (i = 1, j = 0; i < 256; i++)
    {
        if (!isalpha(i)) delim[j++] = i;
    }
    return delim;
}

int main() {
    int i = 0, cur = 0;
    FILE    *pFile;
    char    readLine[1024], *ptr;
    char *token = MakeToken();

    InitHeap();

    pFile = fopen("C:\\Users\\Home\\Desktop\\dataset.txt", "r");
    if (pFile == NULL) {
        printf("File open failed.\n");
        return 0;
    }
    while (fgets(readLine, 1024, pFile) != NULL) {
        ptr = strtok(readLine, token);
        while (ptr != NULL) {
            InsertHeap(ptr);
            ptr = strtok(NULL, token);
        }
    }

    for (i = 0; i < heapSize; i++)
    {
        cur = DeleteHeap();
        printf("%s %d\n", heap[cur].words, heap[cur].count);
    }

    return 0;
}

错误消息:运行时错误#3 我想制作从文件中读取txt的程序,将它们剪切成单词,然后在控制台上显示。我做到了,但它不起作用。怎么解决? 我认为静态WORDS堆&lt; - 这一部分 要么 删除部分是错误的。 或者它的路径是失败的。

2 个答案:

答案 0 :(得分:0)

我在您的代码中看到以下错误:

heap[0].words = NULL;

单词是一个数组,而不是一个dynamyc分配的指针,所以你不能分配给NULL(你得到一个编译器错误!在我看来,WORDS.word变量声明是不正确的。)

strcpy(minElement, heap[1].words);
strcpy(lastElement, heap[heapSize--].words);

minElement和lastElement未初始化且未分配,因此strcpy函数将失败。

这是一种纠正代码的方法,我改变了最小可能。以下收集单词,计算出现次数并按字母顺序打印结果:

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

typedef struct _WORDS { 
    char words[64]; 
    int count;
} WORDS;


static WORDS heap[10000];

int heapSize;

void InitHeap()
{
    heapSize = 0;
}

void InsertHeap(char* string)
{
    int num = 0;

    // Search string in heap array. if found, increase count.
    for(num = 0; num < heapSize; ++num)
    {
        if(strcmp(string, heap[num].words) == 0)
        {
            heap[num].count++;
            return;
        }
    }

    // If not found, add it to the array.
    strcpy(heap[heapSize].words, string); 
    heap[heapSize].count = 1;
    heapSize++;
}

char* MakeToken(void)
{
    int i, j;  
    static char delim[256];
    memset(delim, 0x0, 256);

    for (i = 1, j = 0; i < 256; i++)
    {
        if (!isalpha(i)) delim[j++] = i;
    }
    return delim;
}

int compare(const void* v1, const void* v2)
{
    return strcmp((const char*)v1, (const char*)v2);
}

int main() 
{
    int i = 0, cur = 0;
    FILE *pFile;
    char readLine[1024], *ptr;
    char *token = MakeToken();

    InitHeap();

    pFile = fopen("C:\\Users\\Home\\Desktop\\dataset.txt", "r");
    if(pFile == NULL) 
    {
        printf("File open failed.\n");
        return 0;
    }

    while (fgets(readLine, 1024, pFile) != NULL) 
    {
        ptr = strtok(readLine, token);
        while (ptr != NULL) 
        {
            InsertHeap(ptr);
            ptr = strtok(NULL, token);
        }
    }

    // Order alphabetically the heap array.
    qsort(heap, heapSize, sizeof(WORDS), compare);

    for (i = 0; i < heapSize; i++)
    {
        printf("%s %d\n", heap[i].words, heap[i].count);
    }

    return 0;
}

答案 1 :(得分:-1)

我修复了代码中的一些错误,并开始产生一些结果。由于我完全不了解你的任务,我无法进一步发展。工作代码如下:

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct _WORDS { char words[64]; int count; }WORDS;

static  WORDS app_heap[10000];
int heapSize;

void InitHeap()
{
    heapSize = 0;
    app_heap[0].words[0] = '\0';
    app_heap[0].count = -1;
}

void InsertHeap(char* string)
{
    heapSize++;
    strcpy(app_heap[heapSize].words, string);

    int now = heapSize;
    while (app_heap[now / 2].words > string)
    {
        app_heap[now] = app_heap[now / 2];
        now /= 2;
    }
    strcpy(app_heap[now].words, string);
}

int DeleteHeap()
{
    char minElement[64], lastElement[64];
    int child, now;

    if(heapSize <= 0)
    {
        printf("Wrong call\n");
        return 0;
    }
    strcpy(minElement, app_heap[1].words);
    strcpy(lastElement, app_heap[heapSize--].words);

    for (now = 1; now * 2 <= heapSize; now = child)
    {
        child = now * 2;
        if (child != heapSize && app_heap[child + 1].words < app_heap[child].words)
        {
            child++;
        }
        if (lastElement > app_heap[child].words)
        {
            strcpy(app_heap[now].words, app_heap[child].words);
        }
        else
        {
            break;
        }
    }
    strcpy(app_heap[now].words, lastElement);

    return now;
}

char* MakeToken(void)
{
    int i, j;
    static char delim[256];
    memset(delim, 0x0, 256);

    for (i = 1, j = 0; i < 256; i++)
    {
        if (!isalpha(i)) delim[j++] = i;
    }
    return delim;
}

int main() {
    int i = 0, cur = 0;
    FILE    *pFile;
    char    readLine[1024], *ptr;
    char *token = MakeToken();

    InitHeap();

    pFile = fopen("dataset.txt", "r");
    if (pFile == NULL) {
        printf("File open failed.\n");
        return 0;
    }
    while (fgets(readLine, 1024, pFile) != NULL) {
        ptr = strtok(readLine, token);
        while (ptr != NULL) {
            InsertHeap(ptr);
            ptr = strtok(NULL, token);
        }
    }

    for (i = 0; i < heapSize; i++)
    {
        cur = DeleteHeap();
        printf("%s %d\n", app_heap[cur].words, app_heap[cur].count);
    }

    return 0;
}