Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 40287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:00:00+00:00 2026-05-10T15:00:00+00:00

I am working with an open-source UNIX tool that is implemented in C++, and

  • 0

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 { 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()); 

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?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:00:01+00:00Added an answer on May 10, 2026 at 3:00 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I've use (and struggled with) DotNetNuke. May 11, 2026 at 2:56 pm
  • added an answer This should get you started: http://weblogs.asp.net/hpreishuber/archive/2008/11/18/rownumber-in-silverlight-datagrid-or-listbox.aspx It says it's for… May 11, 2026 at 2:56 pm
  • added an answer Try using f_frsize instead of f_bsize. >>> s = os.statvfs('/')… May 11, 2026 at 2:56 pm

Related Questions

I am working with an artist to make her personal website. She would like
I am looking for an open-source project involving c++ GUI(s) working with a database.
I am considering helping an open-source gaming project with creating their setup file. The
I'm working with a group of other programmers on an open source project built

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.