开发者

formatted output to a C++ ostringstream object

开发者 https://www.devze.com 2023-02-15 00:36 出处:网络
cout << setw(20) << setiosflags(std::ios_base::left) << stamp; // this is char stamp[200] dataype.
cout << setw(20) << setiosflags(std::ios_base::left) << stamp; // this is char stamp[200] dataype.
cout << setw(1)  << setiosflags(std::ios_base::left) << " ";
cout << setw(10) << setiosflags(std::ios_base::left) << appName1; // this is stl string data dtype
cout << setw(1)  << setiosflags(std::ios_base::left) << ":";
cout << setw(15开发者_开发百科) << setiosflags(std::ios_base::left) << myname; // this is stl string data type.
cout << setw(1)  << setiosflags(std::ios_base::left) << ":";    
cout << setw(10) << setiosflags(std::ios_base::left) << myotherappname; // this is stl string data type.
cout << endl;
cout << finallyMyMessage; // this is char finallyMyMessage[200]; 

now my requirement is to put the above formatted text to file also in addition to console. I know that we can do this with ostringstream, but not able to sampel code how we can do this. Can any one help me in providing an exampel code how we can do this using ostringstream, later i can write to file using using ostreamobject.

Thanks!


Streams are polymorphic. They all derive from a common base class. That common base class defines most operations. Derived classes just setup the right stream buffer.

So use them polymorphically:

void write(std::ostream& os)
{
  os << setw(20) << setiosflags(std::ios_base::left) << stamp;
  os << setw(1)  << setiosflags(std::ios_base::left) << " ";
  os << setw(10) << setiosflags(std::ios_base::left) << appName1;
  os << setw(1)  << setiosflags(std::ios_base::left) << ":";
  os << setw(15) << setiosflags(std::ios_base::left) << myname;
  os << setw(1)  << setiosflags(std::ios_base::left) << ":";    
  os << setw(10) << setiosflags(std::ios_base::left) << myotherappname;
  os << endl;
  os << finallyMyMessage;
}

// ...

write(std::cout);

std::ostringstream oss;
write(oss);

std::ofstream ofs("file.txt");
write(ofs);


If you have boost available, you can use the boost format library:

#include <boost/format.hpp>

cout << format("%20s %10s %15s %10s") % stamp % appName1 % myname % myotherappname << endl;

you can send the output to any ostream-derived type.


What you actually need is an ofstream to write to a file. Writing to a ostringstream first and then writing that string to both console and file may indeed be faster, but let's start with how to write to a file:

ostream& st = use_file? ofstream("output.txt"): cout;

ofstream st("output.txt");

st << setw(20) << setiosflags(std::ios_base::left) << stamp; // this is char stamp[200] dataype.
st << setw(1)  << setiosflags(std::ios_base::left) << " ";
st << setw(10) << setiosflags(std::ios_base::left) << appName1; // this is stl string data dtype
st << setw(1)  << setiosflags(std::ios_base::left) << ":";
st << setw(15) << setiosflags(std::ios_base::left) << myname; // this is stl string data type.
st << setw(1)  << setiosflags(std::ios_base::left) << ":";    
st << setw(10) << setiosflags(std::ios_base::left) << myotherappname; // this is stl string data type.
st << endl;
st << finallyMyMessage; // this is char finallyMyMessage[200]; 


You can use the OS to do this.

On unix you have tee

./a.out | tee plop.txt

Dumps output to file "plop.txt" and console.


Pretty straightforward really...

#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

int main(int argc, char* argv[])
{
    std::ostringstream oss;

    // defined just so my example will compile
    std::string stamp, appName1, myname, myotherappname, finallyMyMessage;

    oss << std::setw(20) << std::setiosflags(std::ios_base::left) << stamp; // this is char stamp[200] dataype.
    oss << std::setw(1)  << std::setiosflags(std::ios_base::left) << " ";
    oss << std::setw(10) << std::setiosflags(std::ios_base::left) << appName1; // this is stl string data dtype
    oss << std::setw(1)  << std::setiosflags(std::ios_base::left) << ":";
    oss << std::setw(15) << std::setiosflags(std::ios_base::left) << myname; // this is stl string data type.
    oss << std::setw(1)  << std::setiosflags(std::ios_base::left) << ":";    
    oss << std::setw(10) << std::setiosflags(std::ios_base::left) << myotherappname; // this is stl string data type.
    oss << std::endl;
    oss << finallyMyMessage; // this is char finallyMyMessage[200]; 

    // ...

    std::ofstream outfile("something.txt");   // open a file for writing
    outfile << oss.str();  // output the ostringstream's string to the ofstream
    outfil.close();

    // or if you need a char*...
    std::string str(oss.str());

    usecharstar(str.c_str());

    return 0;
}


You don't have to repeat the left adjust for each output, it sticks to the stream.

You also don't have to change width for each output, as it sets the minimum width for the field. That makes width(1) totally meaningless for a single character output.

This is easy. You are just trying too hard!

0

精彩评论

暂无评论...
验证码 换一张
取 消