sprintf equivalent in c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A.Leopold

    #1

    sprintf equivalent in c++

    hello,

    is there a c++ equivalent function to the c-function 'sprintf'?

    Thank you,

    leo

  • Pete Becker

    #2
    Re: sprintf equivalent in c++

    On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopol d@himt.desaid:
    >
    is there a c++ equivalent function to the c-function 'sprintf'?
    >
    Yes. Its name is sprintf.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)

    Comment

    • A.Leopold

      #3
      Re: sprintf equivalent in c++

      ;)
      ok, I asked, cause my compiler (VS05) is complaining about the use of
      sprintf ....

      Pete Becker schrieb:
      On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopol d@himt.desaid:
      >
      >>
      >is there a c++ equivalent function to the c-function 'sprintf'?
      >>
      >
      Yes. Its name is sprintf.
      >

      Comment

      • juanvicfer

        #4
        Re: sprintf equivalent in c++

        On Oct 9, 3:33 pm, "A.Leopold" <andreas.leop.. .@himt.dewrote:
        hello,
        >
        is there a c++ equivalent function to the c-function 'sprintf'?
        Maybe you want to use stringstream.
        >
        Thank you,
        >
        leo

        Comment

        • A.Leopold

          #5
          Re: sprintf equivalent in c++



          juanvicfer schrieb:
          On Oct 9, 3:33 pm, "A.Leopold" <andreas.leop.. .@himt.dewrote:
          >hello,
          >>
          >is there a c++ equivalent function to the c-function 'sprintf'?
          >
          Maybe you want to use stringstream.
          ok, I will have a look at stringstream

          Comment

          • sean_in_raleigh@yahoo.com

            #6
            Re: sprintf equivalent in c++

            On Oct 9, 9:33 am, "A.Leopold" <andreas.leop.. .@himt.dewrote:
            hello,
            >
            is there a c++ equivalent function to the c-function 'sprintf'?

            You can also check out boost::format, which can be
            easier to work with than stringstream:



            Sean

            Comment

            • A.Leopold

              #7
              Re: sprintf equivalent in c++



              sean_in_raleigh @yahoo.com schrieb:
              On Oct 9, 9:33 am, "A.Leopold" <andreas.leop.. .@himt.dewrote:
              >hello,
              >>
              >is there a c++ equivalent function to the c-function 'sprintf'?
              >
              >
              You can also check out boost::format, which can be
              easier to work with than stringstream:
              >

              >
              Sean
              thank you, that looks much more convenient!

              Comment

              • Pascal J. Bourguignon

                #8
                Re: sprintf equivalent in c++

                "A.Leopold" <andreas.leopol d@himt.dewrites :
                ;)
                ok, I asked, cause my compiler (VS05) is complaining about the use of
                sprintf ....
                I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h/ }).

                But if you want some safety, you'd use ::snprintf, and if you don't want to implement
                the safety yourself, you could rather use lisp^W std::ostringstr eam.

                std::ostringstr eam s; s<<"Hi "<<world<<std:: endl;
                std::string str =s.str();
                const char* cstr=s.c_str();

                --
                __Pascal Bourguignon__

                Comment

                • Lionel B

                  #9
                  Re: sprintf equivalent in c++

                  On Thu, 09 Oct 2008 17:12:56 +0200, A.Leopold wrote:
                  sean_in_raleigh @yahoo.com schrieb:
                  >On Oct 9, 9:33 am, "A.Leopold" <andreas.leop.. .@himt.dewrote:
                  >>hello,
                  >>>
                  >>is there a c++ equivalent function to the c-function 'sprintf'?
                  >>
                  >>
                  >You can also check out boost::format, which can be easier to work with
                  >than stringstream:
                  >>
                  > http://www.boost.org/doc/libs/1_36_0...oc/format.html
                  >>
                  >Sean
                  >
                  thank you, that looks much more convenient!
                  There is also boost::lexical_ cast (something like a "cheap and cheerful"
                  version of stringstream formatting):



                  --
                  Lionel B

                  Comment

                  • Default User

                    #10
                    Re: sprintf equivalent in c++

                    Pascal J. Bourguignon wrote:
                    "A.Leopold" <andreas.leopol d@himt.dewrites :
                    >
                    ;)
                    ok, I asked, cause my compiler (VS05) is complaining about the use
                    of sprintf ....
                    >
                    I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h>
                    / }).
                    What would make you guess something like that?




                    Brian

                    Comment

                    • Pascal J. Bourguignon

                      #11
                      Re: sprintf equivalent in c++

                      "Default User" <defaultuserbr@ yahoo.comwrites :
                      Pascal J. Bourguignon wrote:
                      >
                      >"A.Leopold" <andreas.leopol d@himt.dewrites :
                      >>
                      ;)
                      ok, I asked, cause my compiler (VS05) is complaining about the use
                      of sprintf ....
                      >>
                      >I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h>
                      >/ }).
                      >
                      What would make you guess something like that?
                      First, AFAIK, C functions are in the "root" namespace; to avoid
                      refering a different object in the current namespace, it's advised (in
                      various style guides) to qualify C functions.

                      Then, using a function without declaring it would make a sane compiler
                      complain, so it's good practice to include some header declaring it
                      before using it.


                      --
                      __Pascal Bourguignon__

                      Comment

                      • Hendrik Schober

                        #12
                        Re: sprintf equivalent in c++

                        Pascal J. Bourguignon wrote:
                        "Default User" <defaultuserbr@ yahoo.comwrites :
                        >
                        >Pascal J. Bourguignon wrote:
                        >>
                        >>"A.Leopold" <andreas.leopol d@himt.dewrites :
                        >>>
                        >>>;)
                        >>>ok, I asked, cause my compiler (VS05) is complaining about the use
                        >>>of sprintf ....
                        >>I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h>
                        >>/ }).
                        >What would make you guess something like that?
                        >
                        First, AFAIK, C functions are in the "root" namespace; to avoid
                        refering a different object in the current namespace, it's advised (in
                        various style guides) to qualify C functions.
                        >
                        Then, using a function without declaring it would make a sane compiler
                        complain, so it's good practice to include some header declaring it
                        before using it.
                        The is the C++ group here, so the C header <stdio.his to be
                        included using <cstdioand its content is accessible only within
                        the 'std' namespace.

                        Schobi

                        Comment

                        • Default User

                          #13
                          Re: sprintf equivalent in c++

                          Pascal J. Bourguignon wrote:
                          "Default User" <defaultuserbr@ yahoo.comwrites :
                          >
                          Pascal J. Bourguignon wrote:
                          "A.Leopold" <andreas.leopol d@himt.dewrites :
                          >
                          ;)
                          ok, I asked, cause my compiler (VS05) is complaining about the
                          use of sprintf ....
                          >
                          I guess it's rather ::sprintf (with extern "C" { / #include
                          <stdio.h/ }).

                          What would make you guess something like that?
                          >
                          First, AFAIK, C functions are in the "root" namespace;
                          Not (at least theoretically) if you use the headers like <cstdio>.
                          However, that's not necessary.
                          to avoid
                          refering a different object in the current namespace, it's advised (in
                          various style guides) to qualify C functions.
                          I've never seen such a thing.
                          Then, using a function without declaring it would make a sane compiler
                          complain, so it's good practice to include some header declaring it
                          before using it.
                          That's correct, of course. However, <stdio.his a standard C++ header.
                          Why would you enclose it in extern "C"?




                          Brian

                          Comment

                          • Jeff Schwab

                            #14
                            Re: sprintf equivalent in c++

                            Pete Becker wrote:
                            On 2008-10-09 09:33:41 -0400, "A.Leopold" <andreas.leopol d@himt.desaid:
                            >
                            >>
                            >is there a c++ equivalent function to the c-function 'sprintf'?
                            >>
                            >
                            Yes. Its name is sprintf.
                            Assuming that line is preceded by "using std::sprintf;".

                            Comment

                            • robertwessel2@yahoo.com

                              #15
                              Re: sprintf equivalent in c++

                              On Oct 9, 8:45 am, "A.Leopold" <andreas.leop.. .@himt.dewrote:
                              ;)
                              ok, I asked, cause my compiler (VS05) is complaining about the use of
                              sprintf ....

                              Please don't top-post.

                              If you're getting the MSVC warning about depreciated functions, you
                              may ignore that, or turn off the warning with the appropriate compiler
                              option, #pragma or #define. Current versions of MSVC warn about the
                              use of many traditional functions which have the potential, if used
                              incorrectly, to lead to buffer overflows. MS has taken a fair bit of
                              heat for this, especially because they chose to describe the functions
                              as "depreciate d" (which means something specific in terms of the
                              language standard), instead of something describing the risk. In most
                              cases they provide a (Microsoft specific) replacement function which
                              has the potential to be used more easily in a safe way (for example,
                              the non-standard sprintf_s is offered as a "safer" replacement for
                              sprintf).

                              The did it to a number of C++ library functions to. For example,
                              basic_string::c opy is "depreciate d" while the non-standard
                              basic_string::_ Copy_s is provided as a "safer" alternative.

                              http://msdn.microsoft.com/en-us/libr...ys(VS.80).aspx

                              Comment

                              Working...