CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Redirect cout to a file
std::ofstream outputFile("output.txt");
std::streambuf* oldCoutBuf = std::cout.rdbuf(); // Save original cout buffer
std::cout.rdbuf(outputFile.rdbuf());
// Print to the file
std::cout << "This message will be written to output.txt" << std::endl;
// Restore original cout buffer
std::cout.rdbuf(oldCoutBuf);
cout << "Message in file" << endl;
return 0;
}
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run