Writing to an output file

I am trying to write data to an output file from a C++ program, but i want this output file to appear on the console also. I think i have the code to make it write to the output file, but i dont know how to include this output file on the console. Anyone got any ideas?
Thanks.
please explain what do you mean by having the output file on console.
cout?
lol cout is the output to console....cout=console output.....>.<
$ ./program.bin | tee file

From some old thread
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 	/** Send the data to all the logged files **/
	class Output{
	private:
		std::vector< std::auto_ptr<std::ofstream> > out;
		Output(const Output &); //when can't allow copy
		Output& operator=(const Output &);
	public:
		Output(){}
		/** types that work with standard streams **/
		template <class T>
		Output& operator<<(const T &obj){
			std::for_each(
				out.begin(),
				out.end(),
				std::bind2nd( insertion<T>(), obj )
			);
			return *this;
		}
		/** repeating for manipulators **/
		typedef std::ostream& (*manip) (std::ostream&);
		Output& operator<<(manip m){
			std::for_each(
				out.begin(),
				out.end(),
				std::bind2nd( insertion<manip>(), m )
			);
			return *this;
		}
		//pretty obvious, don't you think?
		void add(const char *filename){
			out.push_back( new std::ofstream(filename) );
		}
	};
Here's part of one I made that will use one command to simultaneously output to the console and file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class logger // Logging class that will write to console and file.
{
public: 
	logger(const char* filename)	{		fout.open(filename);	}
	~logger()	{	fout.flush();		fout.close();		}
	
	template<class T>
	friend logger& operator<<(Logger& log, const T& output);
	friend logger& operator<<(logger& log, std::ostream&  (*manip)(std::ostream &));
	friend logger& operator<<(logger& log, std::ios_base& (*manip)(std::ios_base&));
private:
	std::ofstream fout;
};

logger& operator<<(logger& log, std::ostream& (*manip)(std::ostream &)) 
{ // This function allows logger to accept std::endl and std::flush
	manip(std::cout);	manip(log.fout);
	return log;
}
logger& operator<<(logger& log, std::ios_base& (*manip)(std::ios_base&)) 
{ // This function allows the logger to accept modifiers: std::hex, std::fixed
	manip(std::cout);	manip(log.fout);
	return log;
}
//This function accepts parameterized modifiers: std::setw(5)
logger& operator<<(logger& log, std::_Smanip<std::streamsize> MySmanip) { return log; }

template<class T> //Accepts strings or values
logger& operator<<(  logger& log,  const T& output  ) 
{
	std::cout << output;
	log.fout << output << std::flush;
	return log;
}


Now just use it like this:
1
2
logger output("Logfile.txt");
output << "text" << std::setprecision(5) << std::hex << 32 << std::endl;
Last edited on
Topic archived. No new replies allowed.