Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Serializing Data to / from an ASCII-file

0.00/5 (No votes)
9 Nov 2000 1  
A series of articles that resulted from experimenting with adding Drag and Drop features to my existing application

This article is part of the drag and drop interface samples.

  1. Serializing ASCII Data
  2. Modeless child dialog
  3. Modeless sibling dialog
  4. The drag source
  5. The MFC drop target
  6. The TBTextTarget class

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)
{
    if (ar.IsStoring())
    {
        for (int i=0; i<m_String.GetUpperBound(); i++)
            ar.WriteString(m_Strings[i]+"\n");
    }
    else
    {
        m_Strings.RemoveAll();

        int idx=0;
        CString t;

        TRY
        {
            while(ar.ReadString(t))
            {
                m_Strings.SetAtGrow(idx, t);
                idx++;
            }
        }
        CATCH(CArchiveException, e)
        {
#ifdef _DEBUG
            TCHAR szCause[255];
            CString strFormatted;
            e->GetErrorMessage(szCause, 255);
            // in real life, it's probably more
            // appropriate to read this from
            // a string resource so it would be easy to
            // localize
            strFormatted = _T("CArciveException: ");
            strFormatted += szCause;
            AfxMessageBox(strFormatted); 
#endif //_DEBUG
        }
        END_CATCH;

        UpdateAllViews(NULL);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here