C 我不知道如何将输入文件中的字符串(单词)读入链表
例如,您可以使用非常低级别的字符扫描,如以下示例中所示-它可以扩展为实际接受多个分隔符、分隔符重复等:C 我不知道如何将输入文件中的字符串(单词)读入链表,c,C,例如,您可以使用非常低级别的字符扫描,如以下示例中所示-它可以扩展为实际接受多个分隔符、分隔符重复等: void printList(struct node* node) { while(node != NULL) { printf(" %s ", node->data); node = node->next; } } #包括 #包括 #包括 结构节点 { 字符*数据; 结构节点*下一步; }; void insertNode
void printList(struct node* node)
{
while(node != NULL)
{
printf(" %s ", node->data);
node = node->next;
}
}
#包括
#包括
#包括
结构节点
{
字符*数据;
结构节点*下一步;
};
void insertNode(结构节点**,字符*);
无效打印列表(结构节点*);
int main()
{
结构节点*head=NULL;
文件*fptr;
字符文件名[20];
char-str[1000];
int numochar;
printf(“输入文件名:”);
scanf(“%s”,文件名);
printf(“输入每行的字符数:”);
scanf(“%d”&numfchar);
fptr=fopen(文件名,“r”);
while(fscanf(fptr,“%s”,str)!=EOF)
{
插入节点(&head,str);
}
fclose(fptr);
印刷品清单(标题);
返回0;
}
void insertNode(结构节点**节点头,字符*数据)
{
结构节点*new_节点=malloc(sizeof*new_节点);
新建节点->数据=strdup(数据);
新建节点->下一步=空;
while(*nodeHead)
节点头=&(*nodeHead)->下一步;
*nodeHead=新节点;
}
无效打印列表(结构节点*节点)
{
while(节点!=NULL)
{
printf(“%s”,节点->数据);
节点=节点->下一步;
}
}
作为第一个近似值,在循环中尝试fscanf(fptr,“%s”,str)
。我在while循环中尝试了它,但它仍然崩溃。当我在while循环中使用fscanf时,insertNode方法似乎崩溃了。我不知道为什么。那么你就没有阅读单词的问题了,你就有把单词插入列表的问题了。尝试将关注点分离。写一个程序,每行打印一个单词。满意的?编写一个程序,在列表中插入三个硬编码字。满意的?将二者结合起来。while(fscanf(fptr,“%s”,tokens)==1){insertNode(&head,tokens);}
我将如何在insertNode方法中使用它?我似乎有一个问题,它崩溃时,试图插入一个节点到链表。感谢您的帮助。这只是一个示例,所以您可能应该将while循环提取到函数中,并在每次printf调用中调用insertNode?请查看更新的示例代码。我已将其更改为读取“test.txt”
文件的内容,并打印每行中的令牌OK谢谢。调用insertNode函数时仍有问题。我似乎得到了正确的标记,只是没有正确地将其添加到链接列表中。仅供参考,我针对/usr/share/dict/words
运行了此代码,这是一个常见的Unix实现提供的单词列表,包含了英语词典中几乎所有的单词(例如,我的Mac在文件中有235886个字。它执行时没有失败。我运行的代码与此之间的唯一区别是:(a)我添加了一个测试以确保fptr
不为空,以及(b)针对EOF的测试(我更喜欢测试==1
)。不管是什么问题,我怀疑这段代码中是否存在。显然,无法打开该文件将是灾难性的。Prolly应该提到,在200K+字符串和一个painter算法下,它花费了永远,但它仍然完成了。
void printList(struct node* node)
{
while(node != NULL)
{
printf(" %s ", node->data);
node = node->next;
}
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE *fptr;
char buf[100];
char *p, *s;
strcpy(buf, "this is just a test");
fptr=fopen("test.txt","r");
while(fgets(buf, sizeof(buf), fptr) != NULL)
{
printf("LINE: %s\n", buf);
/* scan characters and print tokens - single space is a separator */
p = s = buf;
while(*p!=0) {
if (*p==' ') {
*p = 0;
printf("TOKEN: %s\n", s);
s = p+1;
}
p++;
}
printf("TOKEN: %s\n", s);
}
fclose(fptr);
return 0;
}
$ cat test.txt
this is test1
this is test2
$ gcc tokens.c && ./a.out
LINE: this is test1
TOKEN: this
TOKEN: is
TOKEN: test1
LINE: this is test2
TOKEN: this
TOKEN: is
TOKEN: test2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char *data;
struct node *next;
};
void insertNode(struct node**, char *);
void printList(struct node*);
int main()
{
struct node *head = NULL;
FILE *fptr;
char file_name[20];
char str[1000];
int numOfChar;
printf("Enter the name of the file: ");
scanf("%s",file_name);
printf("Enter the number of characters per line: ");
scanf("%d",&numOfChar);
fptr=fopen(file_name,"r");
while(fscanf(fptr, "%s ", str) != EOF)
{
insertNode(&head, str);
}
fclose(fptr);
printList(head);
return 0;
}
void insertNode(struct node** nodeHead, char *data)
{
struct node* new_node = malloc(sizeof *new_node);
new_node->data = strdup(data);
new_node->next = NULL;
while (*nodeHead)
nodeHead = &(*nodeHead)->next;
*nodeHead = new_node;
}
void printList(struct node* node)
{
while(node != NULL)
{
printf(" %s ", node->data);
node = node->next;
}
}