954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

write in file and screen?

how to write into the file at the same time display at screen?if there a command that do it?

amen
Newbie Poster
17 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

I think there there is a *nix shell conmmand that will do it (tee ??) but not in MS-Windows. you might put the output lines into a function then call it twice

void foo(ofstream& out)
{
   out << "Hello World " << endl;
}

int main()
{
   ofstream out("file.txt");
   foo(out);
   foo(cout);
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>if there a command that do it?
No, the framework doesn't exist for this, so you would need to add it on your own. You *can* do it with standard streams such that a single write statement copies to a file or vector of files that you choose, but that's an extremely advanced IOStreams technique that involves extending your own tee'd stream, and it's much easier to just explicitly do two writes like Ancient Dragon showed.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You