CString porble
-
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.
-
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.
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:
-
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:
-
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.
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.