C语言 链表仅抓取文件内容的最后输入



我正在尝试将列表从外部文件上传到链表中,并让列表中可以访问 FILE 中的所有字符。到目前为止,我有这个,但它只包含我的输入文件列表中的最后一个单词。我不知道问题是什么,也不确定从这里开始。任何帮助都会很棒!谢谢。

我上传的.txt文件只是一堆废话,例如:

Ted
Greg
Shoe
Money
Apple

当我运行程序时,列表将只包含苹果。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 15
#define INPUT 1
#define OUTPUT 2
struct tree_node{
  char string[MAXLEN+1];
  struct tree_node *left_child, *right_child;
}*first = NULL;
int main(int argc, char *argv[])
{
  char cell[MAXLEN];
  int op;
  FILE *pf;
  FILE *out;
  pf = fopen(("%s", argv[INPUT]), "r");
        if(pf == NULL){
          fprintf(stderr, "Error: File is Empty. n");
          return 0;
          }else{
  struct tree_node *temp;
  struct tree_node *nn=(struct tree_node*)malloc(sizeof(struct tree_node));
   while(!feof(pf)){
     // fgets(&nn->string, MAXLEN, pf);
    fscanf(pf, "%s", &nn->string);  //I think this is where the problem is.
     if(first != NULL){
       temp = first;
       while(temp -> right_child != NULL)
         temp = temp -> right_child;
           temp -> right_child = nn;
     }else{
       first = nn;
     }
   nn->right_child = NULL;
  }
}
   do{
      printf("1.Display.n2.Exit.n");
      printf("Selection?n");
      scanf("%d", &op);
    switch(op)
      {
       case 1:display();
        break;
      }
    }
   while(op < 2 && op > 0);
}
int display()
{
  struct tree_node *temp;
  temp = first;
  if(temp == NULL)
    {
      printf("EMPTY!n");
      return;
    }
  printf("Elements: n");
  while(temp != NULL){
    printf("%sn", temp -> string);
    temp = temp -> right_child;
  }
}

fscanf(pf, "%s", &nn->string); 这里string是字符数组。 所以删除&.

您只创建了一个节点。上面的这一节 while()。

struct tree_node *temp;
struct tree_node *nn=(struct tree_node*)malloc(sizeof(struct tree_node));

需要为每个字符串创建单独的节点。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 15
#define INPUT 1
#define OUTPUT 2
#define EXIT 2
#define _S(x) #x
#define S(x) _S(x)
struct tree_node{
    char string[MAXLEN+1];
    struct tree_node *left_child, *right_child;
}*first = NULL;
void display(void);
void add_tree(char string[MAXLEN+1]);
int main(int argc, char *argv[]){
    char cell[MAXLEN+1];
    int op;
    FILE *pf, *out;
    if(NULL==(pf = fopen(argv[INPUT], "r"))){
        fprintf(stderr, "Error: File can't open.n");
        return 1;
    }
    while(fscanf(pf, " %" S(MAXLEN) "[^n]", cell)==1){
        add_tree(cell);
    }
    fclose(pf);
    do{
        printf("1.Display.n2.Exit.n");
        printf("Selection?n");
        scanf("%d", &op);
        switch(op){
        case 1:
            display();
            break;
        }
    } while(op != EXIT);
    //release tree
    return 0;
}
struct tree_node* new_node(char string[MAXLEN+1]){
    struct tree_node *np = malloc(sizeof(*np));
    if(np){
        strcpy(np->string, string);
        np->left_child = np->right_child = NULL;
    }
    return np;
}
void insert(struct tree_node **np, char string[MAXLEN+1]){
    int cmp;
    if(*np == NULL){
        *np = new_node(string);
        return;
    }
    if(0==(cmp=strcmp((*np)->string, string)))
        return;
    if(cmp > 0)
        insert(&(*np)->left_child, string);
    else
        insert(&(*np)->right_child, string);
}
void add_tree(char string[MAXLEN+1]){
    insert(&first, string);
}
void display_r(struct tree_node *np){
    if(np){
        display_r(np->left_child);
        printf("%sn", np->string);
        display_r(np->right_child);
    }
}
void display(void){
    if(first == NULL){
        printf("EMPTY!n");
        return;
    }
    printf("Elements: n");
    display_r(first);
}

相关内容

最新更新