fileopen打开文件出错代码void CTextDoc::OnFileOpen() 
{
// TOD Add your command handler code here
CFileDialog dlg(TRUE,NULL,"*.txt");
if(dlg.DoModal()==IDOK)
{
  sFilePathName=dlg.GetPathName();
  if(m_file.Open(sFilePathName,CFile::modeRead))
  {
   CString m_str;
   BOOL m_noEOF = TRUE;
   m_stringArray.RemoveAll();
   do{
    m_noEOF = m_file.Read(&m_str,10);
    
    if(m_noEOF)
    {
     m_stringArray.Add(m_str);
    }
   }while(m_noEOF);
   m_file.Close();
   SetTitle(sFilePathName);//设定标题为打开的文件名
   //MessageBox("file open succeed from View Class"+sFilePathName);
  }
  
}
else
{
  AfxMessageBox("File "+sFilePathName+" Open Error!!");
}
UpdateAllViews(0);
}
不道错在哪里?一运行程序点打开文件后就提示执行错误被关闭。

解决方案 »

  1.   

    void CTextView::OnDraw(CDC* pDC)
    {
    CTextDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    /*CString str("中华人民共和国");
    pDC->TextOut(20,30,str);
    str.LoadString(IDS_TEST01);
    pDC->TextOut(100,200,str);*/
    int m_numLines = pDoc->m_stringArray.GetSize();
    CClientDC dc(this);
    TEXTMETRIC tm;
    dc.GetTextMetrics(&tm);
    int m_charHeight = tm.tmHeight + tm.tmExternalLeading;
    for(int x=0;x<m_numLines;++x)
    {
    CString str=pDoc->m_stringArray.GetAt(x);
    pDC->TextOut(20,x*m_charHeight,str);
    }
    }
      

  2.   

    do{
        m_noEOF = m_file.Read(&m_str,10);不正确用缓冲区来读取数据   char buf[10];
       do{
        m_noEOF = m_file.Read(buf,10);
        m_str = buf;
      

  3.   

    感谢xing_xing_xing(未名) 朋友按你的方法我可以显示出文本文件的内容但如果一个内容没有一行的话会多一个“汤”字来把空的给填为一行而是不空白这是为什么呀?
      

  4.   

    因为buf不是空的,在Debug下是0xcc
    加下面一句清空
    memset(buf,0,10 * sizeof(char));