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 porble

CString porble

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithms
4 Posts 3 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.
  • M Offline
    M Offline
    m1m2
    wrote on last edited by
    #1

    Hi, i have one text file, about 600-700 characters long, I try to read it in one CStfing variable. My code is : CString strTemp; while( ar.ReadString(strTemp) ) m_strFileBuffer += strTemp + _T(" "); ar is CArchive, i try to put all the data from the file in m_strFileBuffer. Problem is that when algorithm pass few times throught ReadString it stop to add data to m_strFileBuffer, it is very strange becouse the data from the file is readed properly in strTemp, then go to next line and try to execute 'm_strFileBuffer += strTemp + _T(" ");' but nothing hapend, algorithm go through this line, back to whili(), read next line from the file and try again to add data to m_strFileBUffer, and that is until ar go to end of the file. At the end i have only half of the file saved in the m_strFileBuffer. Anyone can help with that : ) Thanks.

    M J 2 Replies Last reply
    0
    • M m1m2

      Hi, i have one text file, about 600-700 characters long, I try to read it in one CStfing variable. My code is : CString strTemp; while( ar.ReadString(strTemp) ) m_strFileBuffer += strTemp + _T(" "); ar is CArchive, i try to put all the data from the file in m_strFileBuffer. Problem is that when algorithm pass few times throught ReadString it stop to add data to m_strFileBuffer, it is very strange becouse the data from the file is readed properly in strTemp, then go to next line and try to execute 'm_strFileBuffer += strTemp + _T(" ");' but nothing hapend, algorithm go through this line, back to whili(), read next line from the file and try again to add data to m_strFileBUffer, and that is until ar go to end of the file. At the end i have only half of the file saved in the m_strFileBuffer. Anyone can help with that : ) Thanks.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Was the file written using CArchive::WriteString()?  If not, then you probably shouldn't try to read it with a CArchive::ReadString(). Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        Was the file written using CArchive::WriteString()?  If not, then you probably shouldn't try to read it with a CArchive::ReadString(). Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        m1m2
        wrote on last edited by
        #3

        Thanks a lot : ) it works fine now.

        1 Reply Last reply
        0
        • M m1m2

          Hi, i have one text file, about 600-700 characters long, I try to read it in one CStfing variable. My code is : CString strTemp; while( ar.ReadString(strTemp) ) m_strFileBuffer += strTemp + _T(" "); ar is CArchive, i try to put all the data from the file in m_strFileBuffer. Problem is that when algorithm pass few times throught ReadString it stop to add data to m_strFileBuffer, it is very strange becouse the data from the file is readed properly in strTemp, then go to next line and try to execute 'm_strFileBuffer += strTemp + _T(" ");' but nothing hapend, algorithm go through this line, back to whili(), read next line from the file and try again to add data to m_strFileBUffer, and that is until ar go to end of the file. At the end i have only half of the file saved in the m_strFileBuffer. Anyone can help with that : ) Thanks.

          J Offline
          J Offline
          Jason Teagle
          wrote on last edited by
          #4

          One possible reason that springs to mind is that the file somehow contains an embedded null. If this is true, strTemp probably *is* being added, but the embedded null stops the variable watch window from displaying the entire string. Also, if you try and use string manipulation routines on m_strFileBuffer, they will be affected by this null equally, and terminate the string early. To test if this is the problem, try modifying your code to this: CString strTemp; const int MAX_LINE_LENGTH_EXPECTED = 1024 ; TCHAR cBuffer[MAX_LINE_LENGTH_EXPECTED + 1]; while( ar.ReadString(cBuffer, MAX_LINE_LENGTH_EXPECTED) != NULL) { if (cBuffer[_tcslen(cBuffer) - 1] != _T('\r') && cBuffer[_tcslen(cBuffer) - 1] != _T('\n') ) { // We may have a prematurely-truncated string. // Or it will be the end of the file {:v) assert(false); <=== put breakpoint on this line. } m_strFileBuffer += strTemp + _T(" "); } If there's an embedded null, the character found at where it believes the end of the string is will *not* be the / it should be. Obviously the genuine end of the file may also not end with CR / LF, so be prepared for that. If you think your file might contain lines longer than 1024, adjust MAX_LINE_LENGTH_EXPECTED as appropriate.

          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