简体   繁体   中英

How do I output the cout output from a method into an ofstream file in c++?

I have a main function that prints a sudoku game to the console as I play. I call a print board method which outputs the board along with a 2D array with the numbers. I've been modifying the game to output to the file after every cout:

cout << "puzzle created" << endl;
output << "puzzle created" << endl;

Then the problem is that there is an error when I try the same with a method:

sudoku.printBoard(); //method to print board to console

I can't simply say:

output << sudoku.printBoard();

and I can't say:

output << "number";     

in the method in the board.cpp file either.

Does anyone know any way around this?

This is the problem with print() member functions, and the reason you shouldn't write them. At least, not like this. You've locked yourself into printing to stdout , and now that you want to print somewhere else instead, you're stuck.

Instead, have a function that prints where you tell it to . This may be stdout or a file or whatever.

The function will be declared like this:

void printBoard(std::ostream& os);

and inside its definition, you will use os rather than std::cout .

Then your code will look like this:

sudoku.printBoard(std::cout);
sudoku.printBoard(output);

It works because both std::cout and output are of types that derive from std::ostream .


If you need the cout variant a lot, and you don't want to provide the argument every time because it's getting messy, simply provide an overload:

void printBoard()
{
    printBoard(std::cout);
}

Now you can still write:

sudoku.printBoard();

Whether this is more or less confusing for developers on your project is for you to decide.


If you don't have other things to print, so that this is the only function of its kind within the type of sudoku , a more idiomatic approach would be to create an operator<< for that type:

std::ostream& operator<<(std::ostream& os, const SudokuBoard& board)
{
    board.printBoard(os);
    return os;
}

Now you can use it like this:

std::cout << sudoku;
output << sudoku;

Pass ofstream as a reference to your funtion or simply return string from your function and print it in main() or callee function.

In 2nd case you go like this

string f()
{
   ostringstream os;
   os<<"whatever I want to print in output";
   ....
   ...
   return os.str();
}
int main() or //womain() from wherever you call
{
   output<<f()<<endl;
}

Make a class that takes two ostreams in the constructor. One of these can be std::cout. Store these in the class as references.

Create output stream operators (<<) and use those to write to the two streams.

To use this in other classes pass it as a reference in their constructor (look up dependency injection).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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