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 9250475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:12+00:00 2026-06-18T10:27:12+00:00

I want to refactor some printf / sprintf / fprintf statements into ostream /

  • 0

I want to refactor some printf/sprintf/fprintf statements into ostream/sstream/fstream statements. The code in question pretty-prints a series of integers and floating-point numbers, using whitespace padding and fixed numbers of decimal points.

It seems to me that this would be a good candidate for a Martin Fowler style writeup of a safe, step-by-step refactorings, with important gotchas noted. The first step, of course, is to get the legacy code into a test harness, which I have done.

What slow and careful steps can I take to perform this refactoring?

  • 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. Editorial Team
    Editorial Team
    2026-06-18T10:27:13+00:00Added an answer on June 18, 2026 at 10:27 am

    Basic mechanics of the conversion:

    • Convert each printf-style clause %w.pf or %w.pe, where w is the field width and p is the number of digits of precision, into << setw(w) << setprecision(p) << fixed.
    • Convert each printf-style clause %wd or %wi, where w is the field width, into << setw(w).
    • Convert "\n" to endl where appropriate.

    Process for printf:

    • Create a char[] (let’s call it text) with enough total width.
    • Convert the printf(...) to sprintf(text, ...), and use cout << text to actually print the text.
    • Complete using the common instructions.

    Process for fprintf:

    • Same as printf, but use the appropriate fstream instead of cout.
      • If you already have an opened C-style FILE object that you do not want to refactor at this time, it gets a little sticky (but can be done).
    • Complete using the common instructions.

    Process for sprintf:

    • If the string being written to is only used to output to a stream in the current context, refer to one of the two refactorings above.
      • Otherwise, begin by creating a stringstream and streaming the contents of the char[] you are writing to into that. If you are still intending to extract a char* from it, you can do std::stringstream::str().c_str().
    • Complete using the common instructions.

    Common instructions:

    • Convert each clause one by one into C++-style.
    • Remove *printf and char[] declarations as necessary when finished.
    • Apply other refactorings, particularly “Extract Method” (Fowler, Refactoring) as necessary.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

actually i refactor some portion of code. what i want to do is to
I want to refactor some code using the Windsor IOC/DI framework, but my issue
I have some code that I want to refactor. I have lots of methods
I want to refactor some of my methods into one method for converting string
I want to refactor some of my code, so I would like to create
I'm doing some work with stats, and want to refactor the following method :
I'm trying to refactor some C++ code due to performance problems, and I'm wondering
I'm quite new to smart pointers and was trying to refactor some existing code
I'm trying to refactor some code here, to make it more manageable and reader-friendly.
I want to refactor some C# modules that read data from SQL Server 2008

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.