如何正确的使用CFile::Open中的参数?

 

This line intentionally left blank.

CFile 使用方法,写 FreePoster 时用来测试的。

  [问题提出]
  我设计了一个从记事本中读数据的程序。将数据显示在视中。
  代码如下:

  void CTry1View::OnShow()
  {  
    // TODO: Add your command handler code here
    CStdioFile file;
    
    CString filename;
    CFileDialog opendlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,".(*.txt)|*.txt|All Files(*.*)|*.*||",NULL);
    if(opendlg.DoModal()==IDOK)
        filename=opendlg.GetPathName();
    if(file.Open(filename,CFile::modeCreate|CFile::modeReadWrite|CFile::typeText)==0)
    {

        AfxMessageBox("error");
        return;
    }
                
        while(file.ReadString(string))
                 {
                     strList.AddTail(string);
         string.ReleaseBuffer();
                 }
        
        flag=true;
        Invalidate();
  }
  结果不但在视中没有任何显示,而且记事本中的数据也全部丢失。变成了一片空白。真是搞不懂了。
  记事本中的数据是我随便写的。如下:
  11
  222
  3333
  44444
  .......

  [解决方法]
  在file.Open(filename,CFile::modeCreate|CFile::modeReadWrite|CFile::typeText)中,CFile::modeCreate去掉,modeCreate的意思是没有此文件就建立,有了此文件,清空文件.

最好是这样file.Open(filename,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::typeText),因为CFile::modeNoTruncate意思是文件即使存在也不清空。 ( lichihang 发表于 2003-12-29 8:33:00)

你可能感兴趣的:(String,command,测试,File,null)