Home > Net >  I am trying to load data from a txt file and load it into a binary search tree, then printing it
I am trying to load data from a txt file and load it into a binary search tree, then printing it

Time:11-10

I am trying to load data from a txt file and load it into a binary search tree, then printing it, expecting the .txt contents as an output. Then, i tried clearing/freeing the tree contents, then printing it again, expecting an empty output.

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

typedef struct account
{
    char *email;
    char *password;
    struct account *left;
    struct account *right;
}
account;

account *insertAccount(account *root, char *email, char *password);
account *createAccountNode(char *email, char *password);
account *loadAccountDat(void);
void clearAccount(account *root);
void printAccountTree(account *root);

// Account bintree
account *accountRoot = NULL;


int main(void)
{
    accountRoot = loadAccountDat();
    printAccountTree(accountRoot);
    clearAccount(accountRoot);
    printAccountTree(accountRoot);
}

account *createAccountNode(char *email, char *password)
{
    account *newNode = (account *) malloc(sizeof(account));
    newNode->email = email;
    newNode->password = password;
    newNode->left = NULL;
    newNode->right = NULL;

    return newNode;
}

account *insertAccount(account *root, char *email, char *password)
{
    if(root == NULL)
    {
        return createAccountNode(email, password);
    }
    if(strcmp(email, root->email) < 0)
    {
        root->left = insertAccount(root->left, email, password);
    }
    else if(strcmp(email, root->email) > 0)
    {
        root->right = insertAccount(root->right, email, password);
    }
    return root;
}

void clearAccount(account *root)
{
    if(root == NULL)
    {
        return;
    }

    clearAccount(root->left);
    clearAccount(root->right);

    free(root);
}

// For testing
void printAccountTree(account *root)
{
    if(root == NULL)
    {
        return;
    }
    printAccountTree(root->left);
    printf("%s || %s\n", root->email, root->password);
    printAccountTree(root->right);
}

account *loadAccountDat(void)
{
    if(checkAccountFile() == 1)
    {
        return NULL;
    }

    account *root = NULL;
    FILE *f = fopen("account.txt", "r");
    char *email = malloc(25 * sizeof(char));
    char *password = malloc(25 * sizeof(char));
    char buffer[255];

    // Scan every line in file and put into binary tree
    while(fgets(buffer, 255, f))
    {
        sscanf(buffer, "%[^;];%[^\n]", email, password);
        root = insertAccount(root, email, password);
    }
    fclose(f);
    return root;
}

After loading and storing the txt file contents into the binary tree with the loadAccountDat function, I tried printing the tree and only get 1 line of data from the original txt file to be printed.

Then, after clearing the tree, I tried printing the tree and i got segmentation fault problem

CodePudding user response:

[Assuming the question is about the crash]

The crash happens because you have undefined behavior in your code.

You free all nodes, but you never set accountRoot to NULL. That means the second call to printAccountTree will dereference an invalid pointer.

  • Related