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. CString::Format()

CString::Format()

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
7 Posts 6 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.
  • O Offline
    O Offline
    Oriented
    wrote on last edited by
    #1

    visual C++ how to add a formatted CString value to the original value, e.g: how to achieve these logical statements?! CString str; str += str.Format(" qer23 %d asdgsgd", var); :omg:

    M G P G 4 Replies Last reply
    0
    • O Oriented

      visual C++ how to add a formatted CString value to the original value, e.g: how to achieve these logical statements?! CString str; str += str.Format(" qer23 %d asdgsgd", var); :omg:

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      str.Format(...) doesn't return a string, it reassigns to str in-place. You'll need to use a second variable for the Format() call and append that to str. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Pinky, are you pondering what I'm pondering? I think so Brain, but how will we fit the hamster inside the accordion?

      1 Reply Last reply
      0
      • O Oriented

        visual C++ how to add a formatted CString value to the original value, e.g: how to achieve these logical statements?! CString str; str += str.Format(" qer23 %d asdgsgd", var); :omg:

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

        You must use another variable for this purpose.

        CString str, strTemp;
        strTemp.Format(" qer23 %d asdgsgd", var);
        str += strTemp;

        :cool: Gurmeet S. Kochar


        If you believe in God, it's because of the Devil

        1 Reply Last reply
        0
        • O Oriented

          visual C++ how to add a formatted CString value to the original value, e.g: how to achieve these logical statements?! CString str; str += str.Format(" qer23 %d asdgsgd", var); :omg:

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          or str.Format("%s qer23 %d asdfgsf",str,var);


          MSN Messenger. prakashnadar@msn.com "If history isn't good, just burn it." - Sidhuism.

          G 1 Reply Last reply
          0
          • P Prakash Nadar

            or str.Format("%s qer23 %d asdfgsf",str,var);


            MSN Messenger. prakashnadar@msn.com "If history isn't good, just burn it." - Sidhuism.

            G Offline
            G Offline
            Graham Bradshaw
            wrote on last edited by
            #5

            This approach won't work. To quote from the MSDN documentation for CString::Format() The call will fail if the string object itself is offered as a parameter to Format. For example, the following code: CString str = "Some Data"; str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list. will cause unpredictable results.

            P 1 Reply Last reply
            0
            • G Graham Bradshaw

              This approach won't work. To quote from the MSDN documentation for CString::Format() The call will fail if the string object itself is offered as a parameter to Format. For example, the following code: CString str = "Some Data"; str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list. will cause unpredictable results.

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              yeah sounds logical, coz its working on the same object, i assumed that a copy of the parameter would be made.


              MSN Messenger. prakashnadar@msn.com "If history isn't good, just burn it." - Sidhuism.

              1 Reply Last reply
              0
              • O Oriented

                visual C++ how to add a formatted CString value to the original value, e.g: how to achieve these logical statements?! CString str; str += str.Format(" qer23 %d asdgsgd", var); :omg:

                G Offline
                G Offline
                Gary R Wheeler
                wrote on last edited by
                #7

                If you are using VS.NET 2002 or later, the CString class includes the member function AppendFormat, which does what you are asking. If you are using VC6, you will need to do something like this:

                CString str,tmp;
                str = ";kj;lak;sajlg";
                tmp.Format(" qer23 %d asdgsgd", var);
                str += tmp;


                Software Zen: delete this;

                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