String is not being directed to Output file (C++)

Joshua John

So I'm trying to direct the String that I get from a function to an output file. What's happening is that the buildTree function is creating a binary tree of vector strings and then after it has finished, it calls the printInorder function to print the function in order. It will correctly print out the tree if I replace file << tree.printVector(tempRoot->data); with cout << tree.printVector(tempRoot->data); But trying to direct the returned string from tree.printVector() doesn't seem to do anything. The file.txt is still blank after run. Thanks for the help

here's the code

#include <iostream>
#include "node.h"
#include "tree.h"
#include "cleanString.h"
#include <string>
#include <fstream>
using std::cout;
using std::endl;

BST tree, *root = nullptr;

void buildTree(std::string name){
    ifstream file;
    file.open(name);
    
    if (file){
        std::string word;
        file >> word;
        word = cleanString(word);

        root = tree.Insert(root, word);

        while (file >> word){
            word = cleanString(word);
            tree.Insert(root, word);
        }

        //tree.Inorder(root);
        printInorder(root);
    } else{
        cout << "not a file" << endl;
    }
    file.close();
}

void printInorder(BST* tempRoot){
    ofstream file;
    std::string vecStrings;
    file.open("file.txt");

    if (!tempRoot) { 
        return; 
    } 
    printInorder(tempRoot->left); 
    vecStrings = tree.printVector(tempRoot->data);
    file << vecStrings << endl;                    // the issue here: wont put string in file.txt
    cout << vecStrings << endl;                    // but this will print

    printInorder(tempRoot->right); 
    
    file.close();
}
void printPreorder(){

}
void printPostorder(){

}
Joseph Larson

Rather than a method that opens and closes the file, how about writing:

std::ostream &operator<<(std::ostream &str, const BST &) { ... }

Then you have a generic print method that you can send to any output stream.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

Reversing a string in C without the output being null

From Dev

Reversing a string in C without the output being null

From Dev

gcc error output not getting directed to file

From Dev

python script output won't be directed to file

From Dev

PBSPro qsub output error file directed to path with jobid in name

From Dev

Why is this string: javax.sql.rowset.serial.SerialClob being included in an output file instead of the actual value

From Dev

Java back slash character not being output in string

From Dev

Not being able to write output to text file in JAVA

From Dev

String Output in a data file

From Dev

Why do the strings output using fprintf end up not being written to the output file if my program is terminated via CTRL-C?

From Dev

Why do the strings output using fprintf end up not being written to the output file if my program is terminated via CTRL-C?

From Dev

Download A Non-Directed File With C# In WPF

From Dev

Parsing an input file to create a directed graph C++

From Dev

Accessing Data from a directed input file in C++ with Visual Studio

From Dev

C# Regex match case - split string and write to file output

From Dev

'&' is being shown as '_' in a string c#

From Dev

Popen in c on Windows: Output not being returned?

From Dev

C++ Output being printed twice

From Dev

Watch file output in bash terminal as it is being written to the file

From Dev

Why is the file being emptied during input/output redirection to the same file?

From Dev

C string input output

From Dev

String input/output in C

From Dev

Rails file field being interpreted as String?

From Dev

CSV file being returned as a single string

From Dev

PHP Concatenate file output to string

From Dev

output value of array to string in file

From Dev

CNAME file not being copied to root output when publishing Pelican blog

Related Related

  1. 1

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  2. 2

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  3. 3

    Reversing a string in C without the output being null

  4. 4

    Reversing a string in C without the output being null

  5. 5

    gcc error output not getting directed to file

  6. 6

    python script output won't be directed to file

  7. 7

    PBSPro qsub output error file directed to path with jobid in name

  8. 8

    Why is this string: javax.sql.rowset.serial.SerialClob being included in an output file instead of the actual value

  9. 9

    Java back slash character not being output in string

  10. 10

    Not being able to write output to text file in JAVA

  11. 11

    String Output in a data file

  12. 12

    Why do the strings output using fprintf end up not being written to the output file if my program is terminated via CTRL-C?

  13. 13

    Why do the strings output using fprintf end up not being written to the output file if my program is terminated via CTRL-C?

  14. 14

    Download A Non-Directed File With C# In WPF

  15. 15

    Parsing an input file to create a directed graph C++

  16. 16

    Accessing Data from a directed input file in C++ with Visual Studio

  17. 17

    C# Regex match case - split string and write to file output

  18. 18

    '&' is being shown as '_' in a string c#

  19. 19

    Popen in c on Windows: Output not being returned?

  20. 20

    C++ Output being printed twice

  21. 21

    Watch file output in bash terminal as it is being written to the file

  22. 22

    Why is the file being emptied during input/output redirection to the same file?

  23. 23

    C string input output

  24. 24

    String input/output in C

  25. 25

    Rails file field being interpreted as String?

  26. 26

    CSV file being returned as a single string

  27. 27

    PHP Concatenate file output to string

  28. 28

    output value of array to string in file

  29. 29

    CNAME file not being copied to root output when publishing Pelican blog

HotTag

Archive