Here is a function to print my binary search node tree,

void printtree(struct node *tree)
{

    if (tree!=NULL)
    {
        printtree(tree->left);
        printf("%s %d\n",tree->word, tree->lineNumber);
        printtree(tree->right);

    }
}

How would I save what I print to a text file???????

Just use fprintf instead of printf. You use fopen and fclose to open and close the file (with a FILE* handle).

I've tried this, and all i get is one word and a number, which is not suppose to be because i have a lot of things in the struct node to save to the file,

void printtree(struct node *tree)
{

    if (tree!=NULL)
    {
        printtree(tree->left);
        printf("%s %d\n",tree->word, tree->lineNumber);
        printtree(tree->right);
        FILE *OutFile = fopen("test.txt","w");
        //Send data to file
        fprintf(OutFile,"%s %d\n",tree->word, tree->lineNumber);
    }
}

Because you're recursively calling printtree(), this means you're also calling fopen() over and over again. As a result, you're overwriting the file every time you print something to it. (The file handle is also not getting closed properly.

I would suggest opening the file outside of printtree(), and modifying the parameters of printtree() so it accepts a FILE* as its second argument, which will represent which file the contents get printed to.

I'm not sure how to do that, any suggestions in coding?

Just split the function into the user-facing function and the recursive function, and pass around the file handle in that function. As so:

void printtree_recursion(FILE* OutFile, struct node *tree)
{
    if (tree!=NULL)
    {
        printtree_recursion(OutFile, tree->left);
        fprintf(OutFile, "%s %d\n",tree->word, tree->lineNumber);
        printtree_recursion(OutFile, tree->right);
    }
}

void printtree(struct node *tree)
{
    FILE *OutFile = fopen("test.txt","w");
    printtree_recursion(OutFile, tree);
    fclose(OutFile);
}

Yes, that work thank you very much, but i still need to output my tree to the console as well, with the above code, it is only saving to the "test.txt" file but not showing on the console.

Also if i was loading in a couple different text files into the trees, how would i save it as different text files base on the text file i load into the tree?

If you also want to allow for output to the console, this is very easy to do because the printf(..) function is just a short-hand for fprintf(stdout,..), where the stdout is the "file handle" for the console output. Here is that version:

void printtree_recursion(FILE* OutFile, struct node *tree)
{
    if (tree!=NULL)
    {
        printtree_recursion(OutFile, tree->left);
        fprintf(OutFile, "%s %d\n",tree->word, tree->lineNumber);
        printtree_recursion(OutFile, tree->right);
    }
}

void printtree_to_file(struct node *tree)
{
    FILE *OutFile = fopen("test.txt","w");
    printtree_recursion(OutFile, tree);
    fclose(OutFile);
}

void printtree_to_console(struct node *tree)
{
    printtree_recursion(stdout, tree);
}

So if I use the function a second time without closing the program, it clashes? do I need to clear memory ? If so, how do I do that?

Here's what I'm doing, loading a few different types of text file into the function to count concordance of that text file, my first go around of one of the text file is good, without closing progrom, i'm loading another text file but it clashes, do i need to clear the memory of that tree first before i load the next text file?

Also, how would I save the tree to a text file base on the text file i'm loading into so i get separate files i.e. "text file going in function is called text1" >> save that to text1.txt" Next file is called "text2" save that as text2.txt.

Also, if you want to recreate the structure of the existing binary tree, use a pre-order walk instead of an in-order walk.

void printtree_recursion(FILE* OutFile, struct node *tree)
{
    if (tree!=NULL)
    {
        fprintf(OutFile, "%s %d\n",tree->word, tree->lineNumber);
        printtree_recursion(OutFile, tree->left);
        printtree_recursion(OutFile, tree->right);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.