我如何才能读取长度未知的行中的每个单词?
我试图使用链接列表来保存包含一段文本的文本文件中的每个单词。因此,每一行都有未知数量的单词,每个单词之间用空格分隔。我想我可以使用 strtok() 和 getline() 来阅读每个单词。但是,该程序仅读取每行的第一个单词,因此我认为可以使用循环来检测文件每行的末尾,以便读取所有单词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
char *word;
struct node *next;
};
//refers to the struct for linked list
typedef struct node link;
//a function to add word to front of linked list
link *addName(char[] word, link *head){
link *temp1;
temp1 = (link*)malloc(sizeof(link));
//add char type word to linked list
temp1->word = strdup(word);
temp1->next = word;
head = temp1;
return head;
}
int main(){
FILE *fO;
fO = fopen("paragraph.data", "r");
int size = 0;
int len = 0;
//initialize it for the getline() and strtok()
char *line = 0;
//use malloc
line = (char*)malloc(sizeof(int));
//loop through the file
while(getline(&line, &size, fO) != -1){
char *word = strtok(line, " ");
printf("the word: %s\n", word);
//while(there is no "\n" detected?){}
word = strtok(NULL, " ");
printf("the word: %s\n", word);
//addName()
}
}
该文件是这样的(例如缩写):
lorem ipsum
dolor
sit amet con sec
euter orci
它可以有任意数量的单词,这让我感到困惑。有谁知道如何让 while 循环检测每行的结尾? 现在它只打印出每行的第一个单词。
the name: lorem
the name: dolor
the name: sit
the name: euter
I was trying to use a linked list to hold every word from a textfile that had a paragraph of text in it. So each line has an unknown number of words on it, each separated by a space. I thought I could use strtok() and getline() to read through each word. However, the program only reads the first word on each line, so I thought I could use a loop to detect the end of each line of the file so that all the words would be read.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
char *word;
struct node *next;
};
//refers to the struct for linked list
typedef struct node link;
//a function to add word to front of linked list
link *addName(char[] word, link *head){
link *temp1;
temp1 = (link*)malloc(sizeof(link));
//add char type word to linked list
temp1->word = strdup(word);
temp1->next = word;
head = temp1;
return head;
}
int main(){
FILE *fO;
fO = fopen("paragraph.data", "r");
int size = 0;
int len = 0;
//initialize it for the getline() and strtok()
char *line = 0;
//use malloc
line = (char*)malloc(sizeof(int));
//loop through the file
while(getline(&line, &size, fO) != -1){
char *word = strtok(line, " ");
printf("the word: %s\n", word);
//while(there is no "\n" detected?){}
word = strtok(NULL, " ");
printf("the word: %s\n", word);
//addName()
}
}
the file is like this(shortened for eg.):
lorem ipsum
dolor
sit amet con sec
euter orci
it could have any number of words which is what makes me confused. Does anyone know how to make the while loop detect the end of each line?
Right now it just prints out the first word of every line.
the name: lorem
the name: dolor
the name: sit
the name: euter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
许多问题...
char[] word
不是有效的 C,并且无法编译temp1->next = word;
成功不编译 -word
是一个char *
指针,而不是指向temp1->next = head;
到链表中strtok
上循环,因此,当然,您只会获得一个[或两个]令牌。main
中,size
必须是size_t
而不是int
——getline
调用甚至无法编译。getline
确实不去除换行符line
必须在循环结束时释放main
中的 code>addNamemalloc
的返回值:我是否强制转换 malloc 的结果?在下面的代码中,我使用 < code>cpp 条件来表示旧代码与新代码:
这是重构的代码。我已将
link
更改为node
以更具描述性。它带有错误和修复注释:这是完全清理的代码:
对于您的示例输入,这里是程序输出:
A number of issues ...
char[] word
is not valid C and won't compiletemp1->next = word;
won't compile --word
is achar *
pointer and not a pointer to a nodetemp1->next = head;
to link the new node into the linked liststrtok
, so, of course, you'll only get one [or two] tokens.main
,size
must be asize_t
and not anint
-- thegetline
call won't even compile.getline
does not strip the newlineline
must be freed at the end of the loopaddName
inmain
malloc
: Do I cast the result of malloc?In the code below, I use
cpp
conditionals to denote old vs. new code:Here is the refactored code. I've changed
link
intonode
to be more descriptive. It is annotated with bugs and fixes:Here is the fully cleaned up code:
For your sample input, here is the program output:
您的主要问题是
line
已分配且size
为零。如果您打算让getline
为该行分配空间,则size
必须为零且line
必须为 null。另外,您应该在循环结束时释放
分配的内存。// addWord(词,头);
printf("单词:%s\n", word);
// addWord(词,头);
}
免费(线路);
}
}
Your main issue is that
line
is allocated andsize
is zero. If you intend to letgetline
allocate space for the line, thensize
must be zero ANDline
must be null. Also, you shouldfree
the allocated memory at the end of the loop.// addWord(word,head);
printf("the word: %s\n", word);
// addWord(word,head);
}
free(line);
}
}