473,856 Members | 1,952 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 3580
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
1794
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
5218
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
2206
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
3514
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
3002
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
3381
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
1512
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
3692
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
4659
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
9923
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
9767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11081
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...
1
10806
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5773
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...
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3214
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.