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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sprintf

sprintf

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 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.
  • S Offline
    S Offline
    styve
    wrote on last edited by
    #1

    Hi! Is there a command that works like sprintf, but it is like strcat? Because I will catenate (probably wrong spelling) some hex numbers to the same char value. Have a nice day, Stefan

    J R D 3 Replies Last reply
    0
    • S styve

      Hi! Is there a command that works like sprintf, but it is like strcat? Because I will catenate (probably wrong spelling) some hex numbers to the same char value. Have a nice day, Stefan

      J Offline
      J Offline
      jan larsen
      wrote on last edited by
      #2

      You can just use sprintf like this:

      char buffer[1024];
      char * tmp;
      tmp = buffer;
      tmp += sprintf(tmp, "Line %d\r\n", 1);
      tmp += sprintf(tmp, "Line %d\r\n", 2);
      tmp += sprintf(tmp, "Line %d\r\n", 3);

      Another, and better, solution is to use a strstream:

      strstream out;
      out<<"Line "<<1<

      "After all it's just text at the end of the day. - Colin Davies

      "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

      1 Reply Last reply
      0
      • S styve

        Hi! Is there a command that works like sprintf, but it is like strcat? Because I will catenate (probably wrong spelling) some hex numbers to the same char value. Have a nice day, Stefan

        R Offline
        R Offline
        Robert A T Kaldy
        wrote on last edited by
        #3

        If you use fixed number of hex numbers, use: sprintf("%c%x%x", 'c', 0x544, 0xbff) or if you use variable number: ostrstream os; os << 'c' << hex << 0x544 << 0xbff << .... and the char* string you obtain by os.str() Robert-Antonio "I launched Norton Commander and saw, drive C: on the left, drive C: on the right...Damn, why I need two drives C:???So I formatted one..."

        1 Reply Last reply
        0
        • S styve

          Hi! Is there a command that works like sprintf, but it is like strcat? Because I will catenate (probably wrong spelling) some hex numbers to the same char value. Have a nice day, Stefan

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Here's a somewhat cheesy example:

          char szBuffer[128];
          for (int x = 0, num = 1; x < 10; x += 2)
          {
          sprintf(&szBuffer[x], "%02x", num);
          num *= 2;
          }


          A rich person is not the one who has the most, but the one that needs the least.

          P 1 Reply Last reply
          0
          • D David Crow

            Here's a somewhat cheesy example:

            char szBuffer[128];
            for (int x = 0, num = 1; x < 10; x += 2)
            {
            sprintf(&szBuffer[x], "%02x", num);
            num *= 2;
            }


            A rich person is not the one who has the most, but the one that needs the least.

            P Offline
            P Offline
            Peter Weyzen
            wrote on last edited by
            #5

            or even another choice: CString S; for ( int x = 0, num = 1; x < 10, x += 2 ) {     S.AppendFormat( _T("%02x"), num );     num *= 2; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [Santa Cruz Networks](http://www.santacruznetworks.com)

            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