Search
 
SCRIPT & CODE EXAMPLE
 

CPP

I/O Redirection in C++

// Cpp program to redirect cout to a file
#include <fstream>
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    fstream file;
    file.open("cout.txt", ios::out);
    string line;
 
    // Backup streambuffers of  cout
    streambuf* stream_buffer_cout = cout.rdbuf();
    streambuf* stream_buffer_cin = cin.rdbuf();
 
    // Get the streambuffer of the file
    streambuf* stream_buffer_file = file.rdbuf();
 
    // Redirect cout to file
    cout.rdbuf(stream_buffer_file);
 
    cout << "This line written to file" << endl;
 
    // Redirect cout back to screen
    cout.rdbuf(stream_buffer_cout);
    cout << "This line is written to screen" << endl;
 
    file.close();
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: sideways triangle c++ xy plane 
Cpp :: . Single-line comments start with two forward slashes (//). 
Cpp :: ordine crescente di numeri indefiniti in c++ 
Cpp :: digits in c++ 
Cpp :: how to install open cv2 in c++ on ubuntu 
Cpp :: https://stackoverflow.comInstance of a Character in a String c++ 
Cpp :: Create an algorithm to identify what is the next largest element on a stack (using stack/queue operations only) INPUT: [ 10, 3, 1, 14, 15, 5 ] OUTPUT: 10 - 14 3 - 14 1 - 14 14 - 15 15 - -1 5 - -1 
Cpp :: c++ to c code converter online 
Cpp :: bnchch 
Cpp :: c++ sort numbers by magnitude/absolute value 
Cpp :: 1822. Sign of the Product of an Array leetcode 
Cpp :: Catcoder mars rover solution in c++ 
Cpp :: dfs in tree using adjacency list 
Cpp :: c++ freecodecamp course 10 hours youtube 
Cpp :: how to extract a bit from a byte in c++ 
Cpp :: cpp Case value is not a constant expression 
Cpp :: how to find the left most bit 1 in binary of any number 
Cpp :: inorder to postorder converter online 
Cpp :: online compiler c++ with big O calculatorhttps://www.codegrepper.com/code-examples/cpp/how+to+convert+string+to+wchar_t+in+c%2B%2B 
Cpp :: multiple inheritance c++ 
Cpp :: program in c++ for simple interest rate 
Cpp :: qt c++ thread example 
Cpp :: cpp-variadics/problem? 
Cpp :: Initialize Vector Iterator with begin() function 
Cpp :: how to define a node in c++ 
Cpp :: choose endianness in cpp 
Cpp :: C++ concept simple requirements 
Cpp :: Check if two stacks are the same using C++ 
Cpp :: error when using template base class members 
Cpp :: jquery ajax post json asp.net core 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =