473,695 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatted output to string

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<flo at>(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
Jul 22 '05 #1
4 3565
Joe C wrote:

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<flo at>(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::ostringstr eam sout;
sout.precision( 4);
sout << "Success\t" ...
display(sout.st r(), true);
Jul 22 '05 #2

"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...
Joe C wrote:
Use a stringstream... it does exactly what you ask.


Thanks Ron. Merry Christmas
Jul 22 '05 #3
Joe C wrote:
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<flo at>(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::ostream 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
Jul 22 '05 #4
Jonathan Mcdougall wrote:
Since your question has already been answered, here's a suggestion. Why
not use a type which either derives from std::ostream 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_ty pe c) {
// do whatever you have to do to output the character
// in c here.
}
Jul 22 '05 #5

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

Similar topics

2
1783
by: Mark | last post by:
How do I output formatted numerical data to an external (text) file. At a minimum I want to specify the number of decimal places to print. Ideally I'd like the type of control the fortran FORMAT statement offers. I'm sure this is easy, but I can't seem to find the answer! I want an output file to look like this: 0.1 0.8575 0.2 12.0900 0.4 345.0000
8
5214
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla = et.ElementTree('some_file.xml') xmlb = et.Element('parent') et.SubElement(xmlb, 'child1') et.SubElement(xmlb, 'child2')
6
2197
by: Jason Heyes | last post by:
I am starting to worry about the performance of formatted read/write operations on data-redundant objects in my program.What can I do to improve this performance should it become an issue? Thanks.
2
3506
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1 Copying a file: WRONG WAY: #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
6
2994
by: Magix | last post by:
Hi, I want to use fprintf to write to a file. My question about the formatted output How can I format so that I can allocate certain width for each %s (Left-aignlied) ? Example: fprintf("%s %s %s %s, name, age, city, status); <-------------------><----------------><----------------><------------->
3
3367
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the following: bold text, italic text, superscript & subscript text, bullet points. The output html is then saved in an XML file as a CDATA section for later use in the application when formatted output is required. I have looked up many sites on the...
1
1504
by: Tarun Mistry | last post by:
Hi everyone, is it possible to return the formatted interpretation of an XmlDocument object? Using an XmlWriter object I can write a formatted document to a file, however I want to store it within a string var and output to the screen. Is this possible? Thanks, Taz
6
3681
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++ strings? Using a string stream and the << operator does not give me the formatting control like sprintf. Or did I miss something.... :-|
4
4655
by: keith | last post by:
I've been beating my head against this for a little while, so perhaps someone can help me out here? The code below should output exactly as follows (the hex data lines up in a fixed font): DATA 0123456789abcdef0123456789abcdef 0123456789abcdef0123456789abcdef and the code does indeed do that when I use the fprintf() call. When I comment that line out and use the iostream line, I get gibberish. I guess I'm just not 'getting' C++...
0
8650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2290
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1987
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.