Void output function for txt

I am only in my second quarter of C ++, so please keep the answers simple ....

I have a rather dirty program with linklist, class and file i / o I did a lot with the program, but I can’t get it for output in .txt Perhaps this was my bad encoding format, since the output hit me when I wrote it. function in question:

tag.display_balance(); 

note that this function is in the list of links (tags) that is part of the class and calls the function (display_balance) to print the outputs.

everything goes to the console just fine. but I don’t know how to save it on .txt several google searches and the forum did not show anything that I can understand. I tried:

ofstream BilloutPut;
BilloutPut.open("BillingStatements.txt");
BilloutPut<< tag.display_balance(); 

which is the only way I learned to output to a file, but since it is a void function, it did not work. I would like to stay away from overload <if possible.

- Thank you for watching

+3
source share
2 answers

If the function performs its own file I / O, it will be tough (therefore, there is the concept of "separation of problems", which seems to be violated here). Overloading operator <<will not help; there is no return value from a function for such an operator to place it anywhere.

If you can change the function, let it take an argument, which is the object ostreamto write to (by default, it can be cout).

, std::cout, cout.rdbuf(newbuffer) std::cout, .

-, freopen dup2 stdout ( ) .

, , (.. cout.rdbuf, freopen dup2), . , , , , - , , .

+6

, , , , ostream.

void linkD:: display_balance() {

ListNode *nodePtr;
nodePtr = head;

while (nodePtr){
    nodePtr->driver.print_balanceReport();
    cout<<endl;
nodePtr = nodePtr->next;
}


void driver::print_balanceReport(){
    nBalance=balance;
    cout<<get_firstName()<<" ";
    cout<<get_lastName()<<" ";
    cout<<get_dLicense()<<" ";
    cout<<get_vLicense()<<" ";
    cout<<get_nBalance()<<" ";
    cout<<get_passed()<<" ";
    cout<<get_differance()<<" ";}

()

void ostream ?? , ?

0

Source: https://habr.com/ru/post/1796439/


All Articles