Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I use std::stringstream to format data to a string like sprintf "%02d"[modified]

How do I use std::stringstream to format data to a string like sprintf "%02d"[modified]

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akira32
    wrote on last edited by
    #1

    I want to use std::stringstream to format data to a string like sprintf "%02d" as below: char szWord[MAX_PATH]; int nValue=3; sprintf(szWord,"%02d",nValue); How do I do that by std::stringstream?

    modified on Friday, April 9, 2010 5:17 AM

    K G 2 Replies Last reply
    0
    • A akira32

      I want to use std::stringstream to format data to a string like sprintf "%02d" as below: char szWord[MAX_PATH]; int nValue=3; sprintf(szWord,"%02d",nValue); How do I do that by std::stringstream?

      modified on Friday, April 9, 2010 5:17 AM

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      Check if io stream manipulators helps: http://www.cplusplus.com/reference/iostream/manipulators/[^]

      1 Reply Last reply
      0
      • A akira32

        I want to use std::stringstream to format data to a string like sprintf "%02d" as below: char szWord[MAX_PATH]; int nValue=3; sprintf(szWord,"%02d",nValue); How do I do that by std::stringstream?

        modified on Friday, April 9, 2010 5:17 AM

        G Offline
        G Offline
        Gwenio
        wrote on last edited by
        #3

        stringstream does not print, but iostream does. Try the cout object as follows:

        int x(3);
        std::cout.setfill('0');
        std::cout.setw(2);
        std::cout << x << endl;//endl causes any following text to goto the a new line.

        stringstream is used similarly, but as I said before it writes to memory, not the console. Edit: did not pick up on the formating earlier.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups