我看过一本书<深入浅出>上面说serilize能够读写文件操作,但是是二进制的,怎么让一个标准appwizard生成的SDI菜单,openfile 弹出对话框,读入存储的是文本文件???

解决方案 »

  1.   

    serialize使用串行化类CArchive访问存储体。你可以读一读关于CArchive的资料。
    文本文件可以用
    CArchive类的WtireString,ReadString来操作。
    不过你要是简单的操作,其实没有必要用CArchive 的。CArchive在读写的内容类型包括链表、结构等比较复杂的方法下面很有好处。一般的文本操作用CStdioFile就可以了。
      

  2.   

    那书上不是有简单的方法吗??
    呵呵。。把View改为EditView另外,不涉及到以像的存取,可以用一般的呀
    CStudioFile
      

  3.   

    把View改为EditView,我已经用了CRecordView了,就是想从文本文件读取数据库的字段信息,而且想用,原有菜单里的openfile对话框,
      

  4.   

    只想要人家的文件对话框呀
    可以用CFileDlg fdlg;
      

  5.   

    只是借用CFileDlg打开文本文件,然后解析它,放入数据库
      

  6.   

    CFile       f;
    CFileDialog dlg(TRUE, ".txt", NULL, OFN_ALLOWMULTISELECT, "Text Files (*.txt)|*.txt||", this);if ( IDOK == dlg.DoModal() ) {
       if ( !f.Open( dlg.GetPathName(), CFile::modeCreate | CFile::modeReadWrite ) ){
          AfxMessageBox("打开文件失败!");
          return;
       }   f.Write(...);
       f.Read(...);   ...
       f.Close();
    }
      

  7.   

    是重载void CMainFrame::OnFileOpen()
    {
    CFile       f;
    CFileDialog dlg(TRUE, ".txt", NULL, OFN_ALLOWMULTISELECT, "Text Files (*.txt)|*.txt||", this);if ( IDOK == dlg.DoModal() ) {
       if ( !f.Open( dlg.GetPathName(), CFile::modeCreate | CFile::modeReadWrite ) ){
          AfxMessageBox("打开文件失败!");
          return;
       }   f.Write(...);
       f.Read(...);   ...
       f.Close();
    }
    }
    是这样吗
      

  8.   

    f.Write(...);
       f.Read(...);   ...
       f.Close();
    这些函数好像不应该在OnFileOpen中调用吧
      

  9.   

    CStdioFile应该在那里创建,又在那里进行解析,又在那里关闭亚
      

  10.   

    To  slhappyls() :
        你是猜的不能在OnFileOpen中用,还是试过,试一下才知道嘛,嘿嘿... 没有问题的,如果写文件或读文件失败,查查MSDN上CFile类中的打开方式,看看是否正确。
      

  11.   

    难道OnFileOpen不是在filedlg结束后,就返回吗
      

  12.   

    BOOL CTickDrawDoc::ReadLine(CArchive& ar)
    {
    if (ar.IsLoading())
    {
    memset(recv_buf,0,sizeof(recv_buf));
    int rev_len = 0;
    char cbyte = 0;
    TRY
    {
    ar >> cbyte;
    //while (cbyte != '{')
    while ((cbyte != '<')&&(cbyte != '/'))
    ar >> cbyte;
    }
    CATCH(CArchiveException,e)
    {
    return FALSE;
    }
    END_CATCH
    TRY
    {
    ar >> cbyte;
    // while (cbyte != '}')
    while ((cbyte != '\r'))
    {
    recv_buf[rev_len++] = cbyte;
    ar >> cbyte;
    }
    }
    CATCH(CArchiveException,e)
    {
    }
    END_CATCH
    }
    else
    return FALSE;
    return TRUE;
    }
    读文本
      

  13.   

    void SaveToFile(const char * FileName)
      {
         ofstream OutFile(FileName);
         if (OutFile)
            OutFile<<要写入的变量;
      }
      

  14.   

    在document类的那里调用SaveToFile
      

  15.   

    CFile在CArchive类里成员变量m_pFile