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. formatting a string

formatting a string

Scheduled Pinned Locked Moved C / C++ / MFC
architecturehelpquestion
9 Posts 8 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.
  • N Offline
    N Offline
    NikoTanghe
    wrote on last edited by
    #1

    I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko

    Y A R T N 5 Replies Last reply
    0
    • N NikoTanghe

      I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko

      Y Offline
      Y Offline
      ykutanoor
      wrote on last edited by
      #2

      USE sprintf rather than printf.

      S N 2 Replies Last reply
      0
      • Y ykutanoor

        USE sprintf rather than printf.

        N Offline
        N Offline
        NikoTanghe
        wrote on last edited by
        #3

        hmm. this will crash too. The question is not: how can I format strings. but how can I prevent myself from crashes due to bad formatting parameters.

        1 Reply Last reply
        0
        • Y ykutanoor

          USE sprintf rather than printf.

          S Offline
          S Offline
          Stlan
          wrote on last edited by
          #4

          Your answer is innoportune. Are you sure you have read NikoTanghe's question ? He obviously used printf as an general example to illustrate the problem. This applies to sprintf, CString::Format, etc. as well.

          1 Reply Last reply
          0
          • N NikoTanghe

            I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko

            A Offline
            A Offline
            Andrew Walker
            wrote on last edited by
            #5

            The standard libraries IOStream libraries. That's what exactly what they're designed for. int i = 1; std::string s = "foo" std::cout << i << s; or in the case of printing to a buffer use std::ostringstream in preference to sprintf


            If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

            T 1 Reply Last reply
            0
            • A Andrew Walker

              The standard libraries IOStream libraries. That's what exactly what they're designed for. int i = 1; std::string s = "foo" std::cout << i << s; or in the case of printing to a buffer use std::ostringstream in preference to sprintf


              If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              As much as I dislike iostreams, I have to agree with Andrew. If the programmers can't get their formats straight then iostreams is an excellent choice. Then again, if your programmers can't get their formats straight, you are going to have much more problems than just printing. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              1 Reply Last reply
              0
              • N NikoTanghe

                I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko

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

                Try Boost format library. It provides type-safe formatting very similar to printf. Robert-Antonio "CRAY is the only computer, which runs an endless loop in just 4 hours"

                1 Reply Last reply
                0
                • N NikoTanghe

                  I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  this can't be correct if intergervalue is effectively an integer... %s is waiting for a char pointer which represent a NULL-terminated C-style string.... to prevent this type of crash as you say, it is first to well name your variables. iValue for an integer for example, pcArray for a char pointer... the second thing is to be attentive when coding or you use cout instead, but that'll be more difficult to catch error...


                  TOXCCT >>> GEII power

                  1 Reply Last reply
                  0
                  • N NikoTanghe

                    I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko

                    N Offline
                    N Offline
                    Navin
                    wrote on last edited by
                    #9

                    As a summary of what the others have written... using printf or its variants, how to prevent a crash if the wrong format specifier is used? The answer is simple: You can't. You will have to use IOStreams, or possibly as another person mentioned, boost. Using iostreams is probably the simplest alternate option. Either that, or make sure your format specifiers always match the data types! Sometimes I feel like I'm a USB printer in a parallel universe.

                    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