【问题标题】:Tokenizing input file into linked list将输入文件标记为链表
【发布时间】:2018-04-11 02:56:07
【问题描述】:

我正在尝试标记输入文件并将其单个单词存储在按字数组织的链接列表中。我一直在努力将标记化的字符串存储到节点中,并且正在努力理解我的tokenizing/inserting 过程中的错误之处。目前,在打印存储的字符串时,每个字符串的第一个字母被截断,并且看似随机的垃圾和每个字符串的结尾。我尝试了以下方法来修复我的错误:

  1. 在标记化后对每个字符串进行空终止(我已将其保留在 我的程序似乎是正确的)
  2. 使用strncpy() 代替new_word->str = str;
  3. 将指向标记化字符串的指针传递给我的插入函数, 而不是仅仅传递字符串本身。

下面是我的代码

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

typedef struct word{ 
    int length; 
    char *str; 
    struct word *left;
    struct word *right; 
    struct word *down;
}word;


void print_list(word **head){ 

    word *temp_traverse = *head;
    word *temp_down;

    for( ; temp_traverse!=NULL; temp_traverse = temp_traverse->right){ 
        temp_down = temp_traverse;
        for( ; temp_down!=NULL; temp_down = temp_down->down){ 
            printf("Count: %d, String: %s\n", temp_down->length, temp_down->str);
        }
    }

}


int is_empty(word **head, word **tail){ 

    if((*head == NULL)||(*tail == NULL))
        return 1;

    return 0;
}

void insert(word **head, word **tail, word *new_word){ 

    if(is_empty(head, tail)){
        (*head) = new_word; 
        (*tail) = new_word;
        return;
    }

    if((new_word->length)<((*head)->length)){ 
        new_word->right = (*head);
        (*head)->left = new_word;
        (*head) = new_word;
        return;
    }

    word *temp = *head;

    while(((temp->right)!=NULL) && ((temp->length)<(new_word->length))) 
        temp = temp->right;

    if((temp->length) == (new_word->length)){
        while(temp->down != NULL)
            temp = temp->down;
        temp->down = new_word;
        return;
    }

    if(temp->right == NULL){
        word* last = (*tail);
        last->right = new_word;
        new_word->left = last; 
        (*tail) = new_word;
        return;
    }

    word* next = temp->right;
    temp->right = new_word;
    next->left = new_word; 
    new_word->left = temp; 
    new_word->right = next;

    return;
}

void create(word **head, word **tail, char **str){ 

    word *new_word = (word*)malloc(sizeof(word));
    int length = strlen(*str);

    if(new_word == NULL){
            fprintf(stderr, "Error creating a new word node.\n");
            exit(0);
        }

    new_word->str = (char*)malloc(sizeof(*str));
    strncpy(new_word->str, *str, length);
    //new_word->str = *str;
    new_word->length = length;
    printf("%s ", new_word->str); //test print

    new_word->left = NULL;
    new_word->right = NULL;
    new_word->down = NULL;

    insert(head, tail, new_word);

    return;
}


void tokenize(word **head, word **tail, char words_buffer[]){ 

    char *cur; 

    cur = strtok(words_buffer, " .,;()\t\r\v\f\n");

    *cur++ = '\0';
    create(head, tail, &cur);

    /* tokenize the next string and reset the "duplicate" variable */
    while((cur = strtok(NULL, " .,;()\t\r\v\f\n")) != NULL){
        //cur = strtok(NULL, " .,;()\t\r\v\f\n"); 
        *cur++ = '\0';      
        if(cur){
            create(head, tail, &cur);
        }
    }

}

int main(int argc, char *argv[]){ 

    FILE *fp;
    word *head = NULL; 
    word *tail = NULL;

    /*if(argc<3){
        printf("Failure: not enough arguments");
        return -1; 
    }*/

    fp = fopen(argv[1], "r");
    fseek(fp, 0, SEEK_END);
    char words_buffer[ftell(fp)+1];
    fseek(fp, 0, SEEK_SET);

    if(fp==NULL){
        printf("Failure: unreadable file");
        return -1;
    }

    while(fgets(words_buffer, sizeof(words_buffer), fp)){
            if(strlen(words_buffer)>1)
                tokenize(&head, &tail, words_buffer);
    }

    //print_list(&head);

    fclose(fp);
    return 0;
} 

我留下了我的测试字符串打印供您参考。您还会注意到我现在不使用print_list,因为我还没有正确存储字符串。

由于最后的垃圾,我假设我错误地使用了指向字符串的指针,或者malloc()ing 空间太大。至于截断,我不确定,但我认为它与我的 *cur++ = '\0'; 行有关。

非常感谢您的帮助,感谢您抽出宝贵时间查看。

【问题讨论】:

    标签: c string pointers linked-list tokenize


    【解决方案1】:

    您没有使用strncpy() 复制整个字符串。

    实际上,当您使用以下方法获取长度时,您复制的字符太少了:

    int length = strlen(*str);

    strncpy() 手册页所述:

    警告:如果 src 的前 n 个字节中没有 null 字节,则放在 dest 中的字符串不会以 null 结尾。

    因此,请确保当您使用对以空字符结尾的字符串进行操作的函数(例如大多数标准库 str*() 函数)时,您使用以下代码来说明 '\0' 终止符:

    int length = strlen(*str) + 1;

    另外,顺便说一句,malloc() 返回的void * 被隐式转换为任何对象指针类型,所以不是:

    word *new_word = (word*)malloc(sizeof(word));

    你应该简单地使用:

    word *new_word = malloc(sizeof(word));

    甚至更好:

    word *new_word = malloc(sizeof *new_word);

    避免由于更改声明中的指针类型而不是malloc()调用而导致的错误。

    sizeof 运算符不计算非可变长度数组表达式,因此这是一种更可靠的获取对象大小的方法。

    编辑

    至于每个字符串的第一个字符丢失,我认为这是由于:

    *cur++ = '\0';
    

    因为这只是无用地将cur[0] 设置为'\0',然后递增指针;字符串现在从单词的第二个字母开始。

    【讨论】:

    • 嗨,乔伊,感谢您正确解决这两个问题。您的修复的后续行动:现在我已经实现了它们,我的程序在读取前两个字符串后崩溃,或者无法创建新节点。这是我在代码中没有使用任何 free() 的问题吗?谢谢
    • 当我测试你的代码时,我没有得到那种行为;所有的单词都打印出来了。你能发布导致它崩溃的文本文件吗?你确定你删除了两个*cur++ = '\0'; 行吗?
    • 那个文本文件对我来说很好;仔细检查您是否删除了两个*cur++ = '\0'; 行。此外,除非您的内存不足,否则缺少 free() 不会让您崩溃,因此这不太可能是问题。
    • 奇怪,我确定我已经删除了这两个,但程序继续崩溃。我今天在有大约 2GB 可用空间的笔记本电脑上运行了 100 次代码,那么我可能已经用完了吗? 编辑:我删除了输入文件的第三行并消除了问题,看来我的标记函数错误地处理了单字符串行。
    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多