开发者

how to read a class object form file (using CFile) in mfc?

开发者 https://www.devze.com 2023-01-28 12:36 出处:网络
class CMyFile { int a; wchar_t str[10]; public: CMyFile() { 开发者_高级运维a = 12; wcscpy(str, L\"ddddd\");

class CMyFile { int a; wchar_t str[10]; public: CMyFile() { 开发者_高级运维 a = 12; wcscpy(str, L"ddddd"); } };

i am writing in following way 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();
    }

but when i reading it is not reading correc t values reading code is given below class CMyFile { int a; //abc obj; wchar_t str[10]; public: 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();
    }


Hi you can use MFC serialization

0

精彩评论

暂无评论...
验证码 换一张
取 消