Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: how to get fprintf results as a std::string w/o sprintf
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>In pseudocode:</p> <pre><code>class A { public: ... void print(FILE* f); B b; ... }; ... void A::print(FILE *f) { std::string s = "stuff"; fprintf(f, "some %s", s); b.print(f); } 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() c.set_foo(a.to_str()); </code></pre> <p>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:</p> <ol> <li><p>Change the calls to fprintf() to sprintf()</p> <ul> <li>I wouldn't have to rewrite any format strings</li> <li>print() could be reimplemented as: fprint(f, this.to_str());</li> <li>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</li> </ul></li> <li><p>Try to catch the results of a.print() in a string stream</p> <ul> <li>I would have to convert all of the format strings to &lt;&lt; output format. There are hundreds of fprintf()s to convert :-{</li> <li>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 <a href="http://synflood.at/blog/index.php?/archives/456-One-word-of-warning-about-stdio_filebuf.html" rel="nofollow noreferrer">this guy says it may be possible</a>).</li> </ul></li> <li><p>Use Boost's string <a href="http://www.boost.org/doc/libs/1_36_0/libs/format/doc/format.html" rel="nofollow noreferrer">format library</a></p> <ul> <li>More external dependencies. Yuck.</li> <li>Format's syntax is different enough from printf() to be annoying: </li> </ul> <p>printf(format_str, args) -> cout &lt;&lt; boost::format(format_str) % arg1 % arg2 % etc</p></li> <li><p>Use Qt's <a href="https://doc.qt.io/qt-5/qstring.html#asprintf%3E" rel="nofollow noreferrer">QString::asprintf()</a></p> <ul> <li>A different external dependency.</li> </ul></li> </ol> <p>So, have I exhausted all possible options? If so, which do you think is my best bet? If not, what have I overlooked?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload