This article is part of the drag and drop interface samples.
- Serializing ASCII Data
- Modeless child dialog
- Modeless sibling dialog
- The drag source
- The MFC drop target
- The TBTextTarget class
Download source files - 24 Kb
Preface
The CArchive object has methods called ReadString and
WriteString. ReadString reads a
complete line stopping at, but not including the Carriage return/Line feed pair. WriteString
puts the specified string value into the archive, but without the CR/LF. ReadString
detects EOF by a returning zero which can be used in a while-conditional.
This is from the sample application, step zero:
void CInterfaceDoc::Serialize(CArchive& ar)
{
<font color="#000080">if</font> (ar.IsStoring())
{
for (int i=0; i<m_String.GetUpperBound(); i++)
ar.WriteString(m_Strings[i]+"\n");
}
<font color="#000080">else</font>
{
m_Strings.RemoveAll();
int idx=0;
CString t;
TRY
{
<font color="#000080">while</font>(ar.ReadString(t))
{
m_Strings.SetAtGrow(idx, t);
idx++;
}
}
CATCH(CArchiveException, e)
{
<font color="#000080">#ifdef</font> _DEBUG
TCHAR szCause[255];
CString strFormatted;
e->GetErrorMessage(szCause, 255);
<font color="#008000"> // in real life, it's probably more
// appropriate to read this from
// a string resource so it would be easy to
// localize</font>
strFormatted = _T("CArciveException: ");
strFormatted += szCause;
AfxMessageBox(strFormatted);
<font color="#000080">#endif </font><font color="#008000">//_DEBUG</font>
}
END_CATCH;
UpdateAllViews(NULL);
}
}