Produce a .txt file

Exactly how would I produce a .txt file of everything my program showed? But I want it to print out everything, even if I type something in.... Any suggestions?

Say



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
main()
{
int b;
int c;
int x;

cout << "Output 1\n\a";
cin >> b;
cin >> c;
cin >> x;
cout << "Weee that did nothing" << "\n" << "\n";




The .txt output

_____________________________________________________________________________________
Output 1



Weee that did nothing
__________________________________________________________________________________________
So there is nothing like a print all?

I had to do each line individually?
I think for that you should wrint your own stream or something like that.
If you want something easier you could create just a simple class and overload the << and >> operators so that it will be able to print both on the console and on a file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct fileandconsole
{
	ofstream file;
	fileandconsole(string filename):file(filename.c_str()){};
};
fileandconsole &operator <<(fileandconsole &fac, string str)
{
	cout << str;
	fac.file << str;
	return fac;
}
int main () {
  
	fileandconsole FaC("Myfile.txt");
	FaC << "Something";
	return 0;
}
It's always possible to do this from the command line:
program >output.txt

User input is a different story. It's input, so of course it won't go to the standard output. If you need to know what the user entered, you'll have to print it back immediately afterwards.
Last edited on
Topic archived. No new replies allowed.