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. CString question

CString question

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 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.
  • B benjnp

    here's a sample line of codes #1 CString strLine; strLine = "Hello"; cout << strLine << endl; output 007706BC #2 CString strLine; strLine = "Hello"; printf("%s\n", strLine); output Hello Why didn't cout display the "Hello" string, but using printf will display the desired output. What's the difference between cout and printf that made the above discrepancy? Thanx :)

    A Offline
    A Offline
    AHawk
    wrote on last edited by
    #3

    CString has the address of the memory location I think.

    H 1 Reply Last reply
    0
    • A AHawk

      CString has the address of the memory location I think.

      H Offline
      H Offline
      hamidreza_buddy
      wrote on last edited by
      #4

      take a look at this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring.3a3a.operator_lpctstr.asp i think u should cast it to (LPCTSTR) cout << (LPCTSTR) Your_CString_Object;

      1 Reply Last reply
      0
      • B benjnp

        here's a sample line of codes #1 CString strLine; strLine = "Hello"; cout << strLine << endl; output 007706BC #2 CString strLine; strLine = "Hello"; printf("%s\n", strLine); output Hello Why didn't cout display the "Hello" string, but using printf will display the desired output. What's the difference between cout and printf that made the above discrepancy? Thanx :)

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #5

        Use GetBuffer to the underlying char *, don't forget to call ReleaseBuffer after. Christian Graus - Microsoft MVP - C++

        1 Reply Last reply
        0
        • B benjnp

          here's a sample line of codes #1 CString strLine; strLine = "Hello"; cout << strLine << endl; output 007706BC #2 CString strLine; strLine = "Hello"; printf("%s\n", strLine); output Hello Why didn't cout display the "Hello" string, but using printf will display the desired output. What's the difference between cout and printf that made the above discrepancy? Thanx :)

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

          benjnp wrote: cout << strLine << endl; use cout << (LPCTSTR)strLine << endl; instead...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          1 Reply Last reply
          0
          • B benjnp

            here's a sample line of codes #1 CString strLine; strLine = "Hello"; cout << strLine << endl; output 007706BC #2 CString strLine; strLine = "Hello"; printf("%s\n", strLine); output Hello Why didn't cout display the "Hello" string, but using printf will display the desired output. What's the difference between cout and printf that made the above discrepancy? Thanx :)

            B Offline
            B Offline
            benjnp
            wrote on last edited by
            #7

            Thanx guys, Now I know what's the difference and what to do. But since it is a console app, i'll use printf instead. :)

            E D 2 Replies Last reply
            0
            • B benjnp

              Thanx guys, Now I know what's the difference and what to do. But since it is a console app, i'll use printf instead. :)

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #8

              benjnp wrote: since it is a console app, i'll use printf otherwise?u'll use cout?:sigh: V

              1 Reply Last reply
              0
              • B benjnp

                Thanx guys, Now I know what's the difference and what to do. But since it is a console app, i'll use printf instead. :)

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

                benjnp wrote: But since it is a console app, i'll use printf instead. This makes no sense at all. Both printf() and cout can be used for console applications.


                "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                B 1 Reply Last reply
                0
                • D David Crow

                  benjnp wrote: But since it is a console app, i'll use printf instead. This makes no sense at all. Both printf() and cout can be used for console applications.


                  "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                  B Offline
                  B Offline
                  benjnp
                  wrote on last edited by
                  #10

                  yup, both cout and printf can be used in console applications but since cout had some trouble or need additional things to consider, I think it will be beneficial to use printf instead

                  D 1 Reply Last reply
                  0
                  • B benjnp

                    yup, both cout and printf can be used in console applications but since cout had some trouble or need additional things to consider, I think it will be beneficial to use printf instead

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

                    benjnp wrote: ...but since cout had some trouble or need additional things to consider... cout had no trouble. It was doing exactly what it was told to do. benjnp wrote: I think it will be beneficial to use printf instead If you consider the lack of about nine characters to be beneficial, you've a great deal to learn. It would be beneficlal to learn how a CString object behaves when used with cout and printf(). You'll be amazed at what you find!


                    "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                    B 1 Reply Last reply
                    0
                    • D David Crow

                      benjnp wrote: ...but since cout had some trouble or need additional things to consider... cout had no trouble. It was doing exactly what it was told to do. benjnp wrote: I think it will be beneficial to use printf instead If you consider the lack of about nine characters to be beneficial, you've a great deal to learn. It would be beneficlal to learn how a CString object behaves when used with cout and printf(). You'll be amazed at what you find!


                      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                      B Offline
                      B Offline
                      benjnp
                      wrote on last edited by
                      #12

                      Got your point. I understood how a CString object works especially when used in cout. But for now, since I'm in a hurry, I'll use printf for the moment.

                      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