gpt4 book ai didi

c - 使用链接列表将文件中的单词读取到动态字符中

转载 作者:行者123 更新时间:2023-11-30 18:12:54 26 4
gpt4 key购买 nike

我正在尝试从文件中读取数据并将数据保存到链接列表中。我们无法将 char 字变成静态 char。我们必须使用 char *word 使其动态地接受任意长度的单词。我无法从文件中读取单词并将其保存到动态字符中。我之前用 static char 做过这件事,这很简单。这是代码。

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

struct node {
char *word;
struct node *next;
};

struct codex {
char *word;
struct codex *next;
};

struct node *loadWords(FILE *stream);

int main() {
struct node *head;
FILE *stream;
head = loadWords(stream);
return 0;
}

struct node *loadWords(FILE *stream) {
struct node *poem;
struct node *temp;
char *words, *currentWord;
size_t chrCount;
stream = fopen("hw8.data", "r");

rewind(stream);

while(!feof(stream)) {
if(temp = (struct node*)calloc(1, sizeof(struct node)) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
getline(&words, &chrCount, stream);
currentWord = strtok(words, " ");
strcpy(temp->word, words);
head->next = temp;
head = head->next;
}

return poem;
}

我该如何动态地执行此操作?

最佳答案

这使用 getline 从文件中读取每一行。如果words设置为NULL并且chrCount设置为零,getline将分配存储文件中的行所需的内存。
当该行被标记化时,一个结构被调用。 strdup 将分配内存来存储 token 并将 token 复制到结构中。
新的结构被添加到链表的末尾。为单词分配的内存被释放,并且为下一行重置单词和 chrCount。
只要 getline 返回大于 0 的值,循环就会继续。
在 main 中,遍历列表并打印每个单词,然后再次遍历,释放分配的内存。

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

struct node {
char * word;
struct node* next;
};

struct node *loadWords(FILE *stream);

int main()
{
FILE *stream = NULL;
struct node *head;
struct node *temp;
struct node *loop;
head = loadWords(stream);
if ( head == NULL) {
return 1;
}
temp = head;//print each word
while ( temp != NULL) {
printf ( "%s\n", temp->word);
temp = temp->next;
}

temp = head;// free memory
while ( temp != NULL) {
loop = temp->next;
free ( temp->word);
free ( temp);
temp = loop;
}
return 0;
}

struct node *loadWords(FILE *stream) {
struct node *loop = NULL;
struct node *temp = NULL;
struct node *head = NULL;
char *words = NULL;
char *currentWord;
size_t chrCount = 0;
if ( ( stream = fopen("hw8.data", "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}

while( getline( &words, &chrCount, stream) > 0) {//read a line from file
currentWord = strtok(words, " ");//get first token
while ( currentWord != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word = strdup ( currentWord);//allocate memory and copy token to word
if ( head == NULL) {
head = temp;//first structure
}
else {
loop = head;
while ( loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = temp;
}
currentWord = strtok(NULL, " ");//next token
}
free ( words);//release memory
chrCount = 0;//so readline will allocate memory for next line
words = NULL;
}
fclose ( stream);
return head;
}

关于c - 使用链接列表将文件中的单词读取到动态字符中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29355467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com