我正在尝试从文件中读取数据并将数据保存到链接列表中。我们无法将char字组成静态字符。我们必须使用char * word使其动态接受任意长度的单词。我无法从文件中读取单词并将其保存到动态char中。我之前使用静态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;
}
我如何动态地执行此操作?
答案 0 :(得分:0)
由于您似乎知道如何分配内存,我将假设问题是您不知道为每个单词分配多少。 (类似的想法适用于阅读行。)
如果您对每个单词的大小有所了解,可以静态分配,然后在读入每个单词后,动态分配所需的正确大小。
否则,您可以在文本中一次读取一个字符,直到您完成一个单词,这样您就可以根据需要增加缓冲区。
答案 1 :(得分:0)
这使用getline从文件中读取每一行。如果words
设置为NULL并且chrCount
设置为零,则getline将分配从文件中存储该行所需的内存。
当线被标记化时,结构是calloc&#d; d。 strdup
将分配内存来存储令牌并将令牌复制到结构中
新结构将添加到链接列表的末尾。释放为单词分配的内存,并为下一行重置单词和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;
}
答案 2 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *word;
struct node *next;
};
//struct codex unused
struct node *loadWords(FILE *stream);
int main(void) {
struct node *head;
FILE *stream = fopen("hw8.data", "r");//No sense to pass arguments If you do not open at caller side
head = loadWords(stream);
fclose(stream);
{//test print and release
struct node *p = head;
while(p){
struct node *temp = p->next;
puts(p->word);
free(p->word);
free(p);
p = temp;
}
}
return 0;
}
struct node *loadWords(FILE *stream) {
struct node *head = NULL, *curr, *temp;
char *words = NULL, *currentWord;
size_t chrCount = 0;
rewind(stream);
while(getline(&words, &chrCount, stream) != -1){
currentWord = strtok(words, " \t\n");
while(currentWord != NULL){
if((temp = calloc(1, sizeof(struct node))) == NULL) {
fprintf(stderr, "ERROR - Could not allocate memory.\n");
exit(EXIT_FAILURE);
}
temp->word = strdup(currentWord);//malloc and strcpy
if(head == NULL){
curr = head = temp;
} else {
curr = curr->next = temp;
}
currentWord = strtok(NULL, " \t\n");
}
}
free(words);
return head;
}
答案 3 :(得分:-1)
解释这个想法:
我的想法是链接名为Word
的链接列表中每个单词的字符。然后,我将每个单词放在另一个名为Node
的链表中。 (当然,一切都将动态分配。)
文字示例:
Hello World,在这里。
我们的算法将执行以下操作:
H->e->l->l->o
- &gt; W->o->r->l->d
- &gt; h->e->r->e
注意:单词由空格或标准标点符号分隔,因此我使用isspace
和ispunct
函数。
这是我的代码:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <ctype.h>
struct Node {
char * word;
struct Node* next;
};
struct Word {
char chr;
struct Word* next;
};
int main (void)
{
static const char filename[] = "C:\\a.txt";
FILE *file = fopen(filename, "r");
Node *list = 0;
Node **last = &list;
Word *word = 0;
int list_size = 0;
int word_size = 0;
if ( file != NULL )
{
int ch, inword = 0;
while ( 1 )
{
ch = fgetc(file);
if ( isspace(ch) || ispunct(ch) || ch == EOF )
{
if ( inword )
{
inword = 0;
char * string = (char *) malloc(word_size+1);
for(int i=word_size-1; i>=0; i--) {
string[i]= word->chr;
Word * aux = word;
word = word->next;
free(aux);
}
string[word_size] = '\0';
Node * aux = (Node *) malloc(sizeof(Node));
aux->word = string;
aux->next = 0;
*last = aux;
last = & aux->next;
word_size = 0;
}
if(ch == EOF)
break;
}
else
{
inword = 1;
Word * aux = word;
word = (Word *) malloc(sizeof(Word));
word->chr = ch;
word->next = aux;
word_size++;
}
}
fclose(file);
for(Node * aux = list ; aux; aux=aux->next) {
puts(aux->word);
}
getchar();
}
return 0;
}
希望我帮助过。
快乐编码:D