473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Re-directing 'cout' to a text file

I'm doing a managed C++ application. Is there a way to re-direct 'cout' to
output destinations other than the 'black console' that comes up?
Specifically, re-direction to a text file?

Thanks in advance! : )

[==Peteroid==]
Nov 17 '05 #1
5 3011
>I'm doing a managed C++ application. Is there a way to re-direct 'cout' to
output destinations other than the 'black console' that comes up?
Specifically, re-direction to a text file?


Doesn't normal command line redirection work?

MyProgram > myfile.txt

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
Hi Dave,

I need to be able to switch the 'cout' output to a text file within the
managed C++ application code. For example, at different execution times I
might want to re-direct this output to different text files. Or maybe to a
printer. And then possibly back to the default 'black console'. That sort of
thing...

[==Peteroid==]

"David Lowndes" <da****@example.invalid> wrote in message
news:es********************************@4ax.com...
I'm doing a managed C++ application. Is there a way to re-direct 'cout' tooutput destinations other than the 'black console' that comes up?
Specifically, re-direction to a text file?


Doesn't normal command line redirection work?

MyProgram > myfile.txt

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Nov 17 '05 #3
Try :-

ofstream f;
f.open("d:\\output.txt");
cout.rdbuf(f.rdbuf());

cout << "hello world 1" << endl;
cout << "hello world 2" << endl;

f.close();

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
http://blog.voidnish.com /* My blog on C++/CLI, MFC, Whidbey, CLR... */
"Peteroid" <pe************@msn.com> wrote in message
news:O9**************@TK2MSFTNGP11.phx.gbl...
I'm doing a managed C++ application. Is there a way to re-direct 'cout' to
output destinations other than the 'black console' that comes up?
Specifically, re-direction to a text file?

Thanks in advance! : )

[==Peteroid==]

Nov 17 '05 #4
>I need to be able to switch the 'cout' output to a text file within the
managed C++ application code.


Does Nish's suggestion solve the problem?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #5
BRG
Peteroid wrote:
I'm doing a managed C++ application. Is there a way to re-direct 'cout' to
output destinations other than the 'black console' that comes up?
Specifically, re-direction to a text file?


Here is an example of how I do this:
-----------------------------
#include <fstream>
#include <iostream>

int main(void)
{ std::filebuf fb; // declare a file buffer
// connect this file buffer to a file for output
if(fb.open("test.data", std::ios::out))
{
// test write to cout (console)
std::cout << "*** write to console ***\n";
// set cout's buffer to the filebuf and
// save cout's existing stream buffer to sb
std::streambuf *sb = std::cout.rdbuf(&fb);
// test write to cout (the file)
std::cout << "*** write to file ***\n";
// restore cout's old buffer
std::cout.rdbuf(sb);
// close the file
fb.close();
}
// test write to cout (console)
std::cout << "*** back to console ***\n";
}
-----------------------------
I am not sure if this will be the same in a managed application though.

Brian Gladman
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
8
by: Lothar Scholz | last post by:
Because PHP5 does not include the mysql extension any more is there a chance that we will see more Providers offering webspace with Firebird or Postgres Databases ? What is your opinion ? I must...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
1
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
2
by: JW | last post by:
I wanted have this as part of a flood control script: <? echo ("Flood control in place - please wait " . $floodinterval . " seconds between postings."); sleep(5); // go back two pages echo...
2
by: Frans Schmidt | last post by:
I want to make a new database with several tables, so I did the following: <?php CREATE DATABASE bedrijf; CREATE TABLE werknemers (voornaam varchar(15), achternaam varchar(20), leeftijd...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.