c-将文件中的单词读取到带有链表的动态字符中



我正在尝试从文件中读取数据,并将数据保存到链接列表中。我们无法将char字转换为静态char。我们必须使用char*word来动态地接受任何长度的单词。我在读取文件中的单词并将其保存到动态字符中时遇到问题。我以前用静态字符做过这件事,这很容易。这是代码。

#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将分配内存来存储令牌,并将令牌复制到结构中
新结构将添加到链接列表的末尾。为字分配的内存将被释放,并为下一行重置字和chrCount
只要getline返回的值大于0,循环就会继续。
主要是遍历列表打印每个单词,然后再次遍历,释放分配的内存。

#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 ( "%sn", 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 filen");
        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;
}

由于您似乎知道如何分配内存,我认为问题是您不知道为每个单词分配多少内存。(类似的想法适用于逐行阅读)。

如果你对每个单词可能有多大有一些想法,你可以静态地分配,然后在每个单词被读入后,动态地分配你需要的正确大小。

否则,您可以一次读取文本中的一个字符,直到完成一个单词,这样您就可以根据需要增加缓冲区。

#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, " tn");
        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, " tn");
        }
    }
    free(words);
    return head;
}

解释这个想法:

我的想法是将每个单词的字符链接到一个名为Word的链表中。然后,我把每个单词放在另一个名为Node的链表中。(当然,所有内容都将动态分配)。

文本示例:

你好,世界,在这里。

我们的算法会做这样的事情:

H->e->l->l->o->W->o->r->l->d->h->e->r->e

注意:单词用空格或标准标点符号分隔,所以我使用了isspaceispunct函数。

这是我的代码:

#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] = '';
               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

最新更新