underspecified underspecified - 1 year ago 186
C++ Question

C++: how to get fprintf results as a std::string w/o sprintf

I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred.

Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer. In its print function, it also recursively calls the identically defined print functions of several member classes ("B" is an example). There is another class C that has a member std::string "foo" that needs to be set to the print() results of an instance of A. Think of it as a to_str() member function for A.

In pseudocode:

class A {

void print(FILE* f);
B b;



void A::print(FILE *f)
std::string s = "stuff";
fprintf(f, "some %s", s);

class C {
std::string foo;
bool set_foo(std::str);


A a = new A();
C c = new C();


// wish i knew how to write A's to_str()

I should mention that C is fairly stable, but A and B (and the rest of A's dependents) are in a state of flux, so the less code changes necessary the better. The current print(FILE* F) interface also needs to be preserved. I have considered several approaches to implementing A::to_str(), each with advantages and disadvantages:

  1. Change the calls to fprintf() to sprintf()

    • I wouldn't have to rewrite any format strings

    • print() could be reimplemented as: fprint(f, this.to_str());

    • But I would need to manually allocate char[]s, merge a lot of c strings , and finally convert the character array to a std::string

  2. Try to catch the results of a.print() in a string stream

    • I would have to convert all of the format strings to << output format. There are hundreds of fprintf()s to convert :-{

    • print() would have to be rewritten because there is no standard way that I know of to create an output stream from a UNIX file handle (though this guy says it may be possible).

  3. Use Boost's string format library

    • More external dependencies. Yuck.

    • Format's syntax is different enough from printf() to be annoying:

    printf(format_str, args) -> cout << boost::format(format_str) % arg1 % arg2 % etc

  4. Use Qt's QString::asprintf()

    • A different external dependency.

So, have I exhausted all possible options? If so, which do you think is my best bet? If not, what have I overlooked?


Answer Source

I am using #3: the boost string format library - but I have to admit that I've never had any problem with the differences in format specifications.

Works like a charm for me - and the external dependencies could be worse (a very stable library)

Edited: adding an example how to use boost::format instead of printf:

sprintf(buffer, "This is a string with some %s and %d numbers", "strings", 42);

would be something like this with the boost::format library:

string = boost::str(boost::format("This is a string with some %s and %d numbers") %"strings" %42);

Hope this helps clarify the usage of boost::format

I've used boost::format as a sprintf / printf replacement in 4 or 5 applications (writing formatted strings to files, or custom output to logfiles) and never had problems with format differences. There may be some (more or less obscure) format specifiers which are differently - but I never had a problem.

In contrast I had some format specifications I couldn't really do with streams (as much as I remember)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download