Formatted output to string

J

Joe C

I've written a console application and would like to isolate all screen
output so that it will be easier to migrate the code to a GUI-type platform
without modification to the base code. As a first step, I've created a
variable:
bool useConsole(true);
then I trap all output like:
if(useConsole){
cout << text;
}

However, I realized that having these traps dispersed throught the code was
a little sloppy, so I wrote a function:
void display(string text; useConsole){
if(useConsole){
cout << text;
}
}

which leads to the somewhat cleaner code where I simply say:
display(text, useConsole);

However, I also have some places in the code where I do things like:
string temp = fname;
cout.precision(4);
cout //<< "Success\t"
<< "Success\t"
<< setfill(' ')
<< setw(5)
<< static_cast<float>(clo)/CLOCKS_PER_SEC << " s\t"
<< ((temp.size() < 18) ? temp : temp.replace(7, (temp.size() - 17),
"\xaf")) // strip middle if too long to fit
<< endl;

My question--how can I dump this formatted output into a string that I can
then send to my display() function?

Thanks.

Merry Christmas
 
R

Ron Natalie

Joe said:
However, I also have some places in the code where I do things like:
string temp = fname;
cout.precision(4);
cout //<< "Success\t"
<< "Success\t"
<< setfill(' ')
<< setw(5)
<< static_cast<float>(clo)/CLOCKS_PER_SEC << " s\t"
<< ((temp.size() < 18) ? temp : temp.replace(7, (temp.size() - 17),
"\xaf")) // strip middle if too long to fit
<< endl;

Use a stringstream...it does exactly what you ask.

#include <sstream>

std::eek:stringstream sout;
sout.precision(4);
sout << "Success\t" ...
display(sout.str(), true);
 
J

Jonathan Mcdougall

Joe said:
I've written a console application and would like to isolate all screen
output so that it will be easier to migrate the code to a GUI-type platform
without modification to the base code. As a first step, I've created a
variable:
bool useConsole(true);
then I trap all output like:
if(useConsole){
cout << text;
}

However, I realized that having these traps dispersed throught the code was
a little sloppy, so I wrote a function:
void display(string text; useConsole){
if(useConsole){
cout << text;
}
}

which leads to the somewhat cleaner code where I simply say:
display(text, useConsole);

However, I also have some places in the code where I do things like:
string temp = fname;
cout.precision(4);
cout //<< "Success\t"
<< "Success\t"
<< setfill(' ')
<< setw(5)
<< static_cast<float>(clo)/CLOCKS_PER_SEC << " s\t"
<< ((temp.size() < 18) ? temp : temp.replace(7, (temp.size() - 17),
"\xaf")) // strip middle if too long to fit
<< endl;

My question--how can I dump this formatted output into a string that I can
then send to my display() function?

Since your question has already been answered, here's a suggestion. Why
not use a type which either derives from std::eek:stream or emulates it so
you can use it as an ordinary stream. You could use the standard
library's way by having a global stream object which gets initialized
somewhere safe.


MyStream out;

int main()
{
out.use_console(true);

out << "hello"; // on console with std::cout

out.use_console(false);
out.set_widget(&my_textbox);

out << "hello"; // appends text in my_textbox
}


Jonathan
 
R

Ron Natalie

Jonathan said:
Since your question has already been answered, here's a suggestion. Why
not use a type which either derives from std::eek:stream or emulates it so
you can use it as an ordinary stream. You could use the standard
library's way by having a global stream object which gets initialized
somewhere safe.


MyStream out;
The best way to do this is to write your own streambuffer and make
MyStream derive from ostream, initializing with this strbuf. If you're
not worried about a lot of data going through, you can implmenet the
string buf with a single method:

int_type overflow(int_type c) {
// do whatever you have to do to output the character
// in c here.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top