Click here to Skip to main content
16,022,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am tying to save the contents of a CListBox into an archive file: Part of my store code:

C++
CFile xycFile(szFilePath, CFile::modeCreate | CFile::modeWrite);
		CArchive ar(&xycFile, CArchive::store);
		// Save the contents of the all channels box
		int Count = m_AllChannels.GetCount();
		ar << Count;
		int n = 0;

		for (int j = 0; j < Count; j++) {
			CString szTemp;
			n = m_AllChannels.GetTextLen(j);
			m_AllChannels.GetText(j, szTemp.GetBuffer(n));
			ar << m_AllChannels.GetItemData(j) << szTemp;
		}


For loading:
C++
CFile xycFile(szFilePath, CFile::modeRead);
CArchive ar(&xycFile, CArchive::load);
int Count;
ar >> Count;
for (int j = 0; j < Count; j++) {
    CString szTemp;
    int iPosn;
    ar >> iPosn >> szTemp;
    m_AllChannels.AddString(szTemp);
    m_AllChannels.SetItemData(iPosn, j);
}


I have debugged the Store section and checked that the values are correct for the ItemData and szTemp but when I load the data, I find that the integer values are correct but the CString szTemp is empty.

What I have tried:

Debugging and checking documentation.

Can anyone advise, many thanks.
Posted
Updated 24-Sep-24 12:01pm
v2
Comments
Rick York 24-Sep-24 22:02pm    
I think I would write each channel's data separately, as independent operations that is. Correspondingly, I would read them independently. I would also adjust your GetText method to handle CStrings correctly, if you are going to use them. I try to avoid them as much as possible. I should also state that I detest CArchive and never use it.

Quote:
m_AllChannels.GetText(j, szTemp.GetBuffer(n));
I would call
C++
szTemp.ReleaseBuffer();
before going on.
 
Share this answer
 
CPallini has already described the main error.
It is also clearly stated in the documentation.
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer to update the internal state of CSimpleStringT before you use any other CSimpleStringT methods.
I also miss the closing of the archive and the file
xycFile.Close();
ar.Close();

This is all the more important if you want to use the file and the archive with the same name each time.
However, I would also recommend separate names for both access directions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900