简体   繁体   English

C fgets()只处理最后一行

[英]C fgets() only handling last line

I am trying to write a program that takes in a .txt file, reads all the lines, then stores each word into a BST. 我正在尝试编写一个接收.txt文件的程序,读取所有行,然后将每个单词存储到BST中。 I then perform an inorder traversal to print the words in alphabetical order. 然后我执行inorder遍历以按字母顺序打印单词。 The program works perfectly if the text file contains only 1 line, but if the text file has more than 1 line there are unexpected results. 如果文本文件只包含1行,则程序可以正常工作,但如果文本文件包含多行,则会出现意外结果。

Example: the text file: 示例:文本文件:

first line
second line

Outputs only: 仅输出:

line
second

Completely losing the first line of the text file. 完全丢失文本文件的第一行。

My main method is: 我的主要方法是:

#define MAX_STR_LEN 1024
int main(int argc, char** argv)
{
  FILE *fptr;
  fptr = fopen(argv[1], "r");

  if(fptr == NULL)
  {
   printf("Error. Could not open file.\n");
   exit(1);
   }

  char line[MAX_STR_LEN];
  char* tok;
  struct BSTnode* theRoot;
  int isRoot = 1;

  while(fgets(line, MAX_STR_LEN, fptr) != NULL)
  {
    tok = strtok(line, " \n");


    while(tok != NULL)
    {
      if(isRoot == 1)
      {
        theRoot = createNode(tok); //creates the root for the BST
         isRoot = 0;
      }
      else
      {
           processStr(&theRoot, tok); //places a new word into the BST
      }

      tok = strtok(NULL, " \n");
    }

  }

  inorder(theRoot); //prints words alphabetically
  fclose(fptr);
  return 0;
}

I stepped through with GDB and when fgets is called for the second time in the while loop, theRoot of the BST is changed and overwritten. 我介绍了GDB,当在while循环中第二次调用fgets时,BST的根被更改并覆盖。 Any advice would be appreciated. 任何意见,将不胜感激。

EDIT: 编辑:

struct BSTnode* createNode(char* word) 
{
    struct BSTnode* temp = malloc(sizeof(struct BSTnode));
    temp->word = word;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

void processStr(struct BSTnode** root, char* word) 
{
    struct BSTnode** node = search(root, word);
    if (*node == NULL) {
        *node = createNode(word);
    }
}

struct BSTnode** search(struct BSTnode** root, char* word) {
    struct BSTnode** node = root;
    while (*node != NULL) {
        int compare = strcasecmp(word, (*node)->word);
        if (compare < 0)
            node = &(*node)->left;
        else if (compare > 0)
            node = &(*node)->right;
        else
            break;
    }
    return node;
 }

You need to duplicate the word in your createNode() , try something like: 您需要在createNode()复制该单词,尝试类似下面的操作:

struct BSTnode* createNode(char* word) 
{
    struct BSTnode* temp = malloc(sizeof(struct BSTnode));
    temp->word = malloc(strlen(word) + 1);
    strcpy(temp->word, word);
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

In your createNode() , word is a pointer to substring in line , another call to fgets() will overwrite the content of this array. 在您的createNode() word是对串的指针line ,其他呼叫fgets()将覆盖此数组的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM