如何在mfc中读取类对象表单文件(使用CFile)?

时间:2010-12-03 10:28:41

标签: mfc

类CMyFile {     int a;     wchar_t str [10]; 上市:     CMyFile()     {         a = 12;         wcscpy(str,L“ddddd”);     } };

我是按照以下方式写作的 CMyFile l_temp;

CFile myFile;
CFileException fileException;

if ( !myFile.Open( pszFileName, CFile::modeCreate |   
    CFile::modeReadWrite, &fileException ) )
{
    TRACE( L"Can't open file %s, error = %u\n",
        pszFileName, fileException.m_cause );
}
else
{
    myFile.Write((wchar_t*)&l_temp,sizeof(CMyFile));
    myFile.Close();
    }

但是当我读它时,它并没有读取正确的值 阅读代码如下 类CMyFile {     int a;     // abc obj;     wchar_t str [10]; 上市:     CMyFile()     {         // a = 12;         // wcscpy(str,L“ddddd”);     } };

CMyFile l_temp;

CFile myFile;
CFileException fileException;

if ( !myFile.Open( pszFileName, CFile::modeCreate |   
    CFile::modeReadWrite, &fileException ) )
{
    TRACE( L"Can't open file %s, error = %u\n",
        pszFileName, fileException.m_cause );
}
else
{

    myFile.Read((wchar_t*)&l_temp,sizeof(CMyFile));//not reading correctly
    myFile.Close();
    }

1 个答案:

答案 0 :(得分:1)

您好,您可以使用MFC serialization