将输入文件标记为链表
Tokenizing input file into linked list
我正在尝试 标记化 一个输入文件并将其各个单词存储在按字数组织的链接列表中。我一直在努力将标记化的字符串存储到节点中,并且在努力理解我的 tokenizing/inserting
过程中有什么不正确的地方。目前,在打印存储的字符串时,每个字符串的第一个字母被截断,并且每个字符串的末尾和看似随机的垃圾。我尝试了以下方法来修复我的错误:
- 标记化后以空值终止每个字符串(我把它留在
我的程序似乎是正确的)
- 使用
strncpy()
代替 new_word->str = str;
- 将指向标记化字符串的指针传递给我的插入函数,
而不是仅仅传递字符串本身。
下面是我的代码:
#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++ = '[=10=]';
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++ = '[=10=]';
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()
使用了太多space。至于截断,我不确定,但我认为它与我的 *cur++ = '[=16=]';
行有关。
非常感谢任何帮助,感谢您抽出宝贵时间查看。
您没有用 strncpy()
.
复制整个字符串
实际上,当您获取长度时,您复制的字符太少了:
int length = strlen(*str);
如 strncpy()
联机帮助页所述:
Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
因此,请确保当您使用对以 null 结尾的字符串进行操作的函数时,例如大多数标准库 str*()
函数,您要考虑 '[=15=]'
终止符:
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++ = '[=10=]';
因为那只是无用地将 cur[0]
设置为 '[=15=]'
,然后递增指针;该字符串现在从您单词的第二个字母开始。
我正在尝试 标记化 一个输入文件并将其各个单词存储在按字数组织的链接列表中。我一直在努力将标记化的字符串存储到节点中,并且在努力理解我的 tokenizing/inserting
过程中有什么不正确的地方。目前,在打印存储的字符串时,每个字符串的第一个字母被截断,并且每个字符串的末尾和看似随机的垃圾。我尝试了以下方法来修复我的错误:
- 标记化后以空值终止每个字符串(我把它留在 我的程序似乎是正确的)
- 使用
strncpy()
代替new_word->str = str;
- 将指向标记化字符串的指针传递给我的插入函数, 而不是仅仅传递字符串本身。
下面是我的代码:
#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++ = '[=10=]';
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++ = '[=10=]';
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()
使用了太多space。至于截断,我不确定,但我认为它与我的 *cur++ = '[=16=]';
行有关。
非常感谢任何帮助,感谢您抽出宝贵时间查看。
您没有用 strncpy()
.
实际上,当您获取长度时,您复制的字符太少了:
int length = strlen(*str);
如 strncpy()
联机帮助页所述:
Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
因此,请确保当您使用对以 null 结尾的字符串进行操作的函数时,例如大多数标准库 str*()
函数,您要考虑 '[=15=]'
终止符:
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++ = '[=10=]';
因为那只是无用地将 cur[0]
设置为 '[=15=]'
,然后递增指针;该字符串现在从您单词的第二个字母开始。