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 insertion operator faulty?

CString insertion operator faulty?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
10 Posts 7 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.
  • I Offline
    I Offline
    Indrawati
    wrote on last edited by
    #1

    Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!

    J T A D S 5 Replies Last reply
    0
    • I Indrawati

      Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      try CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << (LPCTSTR)cstr; fTest.close(); Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      1 Reply Last reply
      0
      • I Indrawati

        Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!

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

        fTest << (LPCTSTR)cstr;


        TOXCCT >>> GEII power

        1 Reply Last reply
        0
        • I Indrawati

          Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!

          A Offline
          A Offline
          Anand Paranjpe
          wrote on last edited by
          #4

          When you typecast object of CString with LPCTSTR, internally overloaded operator LPCTSTR will get called. else explicily call function of CString class "operator LPCTSTR()" This will convert CString object to Long Pointer To Constant String which is acceptable format for ofstream. The chosen One :)

          1 Reply Last reply
          0
          • I Indrawati

            Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!

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

            Hmmm, I tried your exact code (without the suggested cast) and it wrote "This is a test" to the file.


            "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

            M 1 Reply Last reply
            0
            • D David Crow

              Hmmm, I tried your exact code (without the suggested cast) and it wrote "This is a test" to the file.


              "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              You're using VS.NET 2002 or 2003, while the OP is using VC6. Something in either the standard library or in CString has changed enough to fix this issue between the two. The changes to CString in MFC 7.x are extensive: it's now defined in an ATL header, for a start. It's also a template class: CString is a typedef for CStringT< StrTraitMFC< TCHAR, ChTraitsCRT< TCHAR > > >. Stability. What an interesting concept. -- Chris Maunder

              D 1 Reply Last reply
              0
              • M Mike Dimmick

                You're using VS.NET 2002 or 2003, while the OP is using VC6. Something in either the standard library or in CString has changed enough to fix this issue between the two. The changes to CString in MFC 7.x are extensive: it's now defined in an ATL header, for a start. It's also a template class: CString is a typedef for CStringT< StrTraitMFC< TCHAR, ChTraitsCRT< TCHAR > > >. Stability. What an interesting concept. -- Chris Maunder

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

                Mike Dimmick wrote: You're using VS.NET 2002 or 2003, while the OP is using VC6... Actually, I've never even seen VS.NET 2002 or 2003, let alone used either of them. I am using VS6.


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                M 1 Reply Last reply
                0
                • D David Crow

                  Mike Dimmick wrote: You're using VS.NET 2002 or 2003, while the OP is using VC6... Actually, I've never even seen VS.NET 2002 or 2003, let alone used either of them. I am using VS6.


                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                  M Offline
                  M Offline
                  Mike Dimmick
                  wrote on last edited by
                  #8

                  I copied the OP's code into a new Win32 Console project in VC6 (selecting 'An application that supports MFC' from the Wizard). It produced the original output. Opening the project in VS.NET 2003, converting it and rebuilding produced the correct output. The fault is that the compiler appears to be selecting the class member (of basic_ostream) operator<<(const void*) for some reason. It's definitely calling CString::operator LPCTSTR to get the pointer whether you include the explicit cast or not. Have you upgraded to Visual Studio 6.0 Service Pack 6, and could this be a solution to the problem? Stability. What an interesting concept. -- Chris Maunder

                  D 1 Reply Last reply
                  0
                  • M Mike Dimmick

                    I copied the OP's code into a new Win32 Console project in VC6 (selecting 'An application that supports MFC' from the Wizard). It produced the original output. Opening the project in VS.NET 2003, converting it and rebuilding produced the correct output. The fault is that the compiler appears to be selecting the class member (of basic_ostream) operator<<(const void*) for some reason. It's definitely calling CString::operator LPCTSTR to get the pointer whether you include the explicit cast or not. Have you upgraded to Visual Studio 6.0 Service Pack 6, and could this be a solution to the problem? Stability. What an interesting concept. -- Chris Maunder

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

                    Mike Dimmick wrote: Have you upgraded to Visual Studio 6.0 Service Pack 6... Yes, I have SP6.


                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                    1 Reply Last reply
                    0
                    • I Indrawati

                      Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!

                      S Offline
                      S Offline
                      shea c4
                      wrote on last edited by
                      #10

                      You have to cast the CString to an LPCTSTR ala fTest << (LPCTSTR)cstr; So that the insertion operator uses the right overload. If you're using unicode, (e.g., L"This is a test" or, _T("This is a test") and unicode is turned on), then you'll want to use a std::wofstream instead of std::ofstream. But you'll still need to cast cstr to LPCTSTR. If you, like me, tried testing this with cout, try using wcout instead (the cast to LPCTSTR is still required).

                      arnshea.blogspot.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