Redirect std out to file

Started by
10 comments, last by Nash 19 years, 10 months ago
Hello, I''m trying to redirect standard out to a file. Here is the situation. I''m using visual C++. I have an existing app which has many print f statemnts. I want the app to now write out a file of all these print statements. I could call CreateFile and then call WriteFile. However, I would need to replace all my print statements with these WriteFile calls. Is there an easier way? Could I use SetStdHandle? Or maybe connect to pipe created by another app. This other app (the pipe server) would do the actual writing file. Any ideas??
Advertisement
Pretty easy if you use the C++ streams.

#include<fstream>std::filebuf f;f.open("myfile.txt", std::ios::out);std::streambuf* buf = std::cout.rdbuf();std::cout.rdbuf(&f);std::cout << "Hello File!" <<std::endl;std::cout.rdbuf(buf);
I want to do the same thing, but redirect all my std::cout to an edit control

- Iliak -
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
If you want to direct std::cout to an edit control, you will need to create your own stream buffer class. In its simplest form you could just derive from the basic_streambuf class and override the virtual overflow(int_type) function to write to the edit control.

[edited by - Jingo on May 26, 2004 2:30:25 PM]
Woohhh !

Any example plz ?

- Iliak -
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
why not just do:


#incldue

ofstream fout;
fout.open("myfile.txt");

/* wrtie to file */
fout << "Hello file!" << endl;

fout.close();



dose it really have to be standard out?

LizardCPP

edit: Ok, I read the OP:s post again, nevermind my post...



[edited by - LizardCPP on May 26, 2004 9:20:21 AM]
The fopen works fine. I''ve used it before. Thanks. However, I would like the app to write to a pipe. Hence, I connect to the pipe with a CreateFile call. Currently, I would have to replace my printf statements with WriteFile statement to get it to work. Is there a way around this? Or must I start replacing printf statments?
freopen("log.txt", "a", stdout);

[edited by - doynax on May 26, 2004 10:28:33 AM]
If it''s just a Win32 console app, what''s wrong with...

myapp.exe > output.txt

...to redirect the printf() output to a file when running the application???

botman
New file needs to be created each day.

This topic is closed to new replies.

Advertisement