刚刚接触MFC不久,现在要做个读取文本文件的并显示在edit控件中的小应用 不需要对话框选择文件路径(路径可直接写在程序里面),只需点击按钮后直接显示数据。时间有点紧。。麻烦各位帮帮忙啊

解决方案 »

  1.   

    FILE *pFile = NULL;
    char szBuf[128];
    memset(szBuf, '\0', sizeof(szBuf));
    int i = 0, nRead = 0, nWrite = 0; memset(szBuf, '\0', sizeof(szBuf));
    if((pFile = fopen("Test.txt", "r+t")) != NULL)
    {
    nRead = fread(szBuf, sizeof(char), sizeof(szBuf) - 1, pFile);
    fclose(pFile);
    szBuf[nRead] = '\0';
    GetDlgItem(edit控件的ID)->SetWindowText(szBuf);
    }仅供参考
      

  2.   

    发你一个我做的读取文本到控件的程序,试验成功
    void CMFCDlg::addFile()
    {
    CFileDialog FileDlg(TRUE, 
    "",
    NULL, 
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "(*.*)||", this);
    if(FileDlg.DoModal()==IDCANCEL)
    {
    return;
    }
    Pathname=FileDlg.GetPathName();
    if (num<0)
    {
    num=0;
    } CString str1;
    CString str2;
    str2.Format("\r\n");
      file.Open(Pathname,CFile::modeRead);
      while (file.ReadString(str1))
      {
       str+=str1+str2;
        }
    EditIn.SetWindowText(str);
      file.Close();
      UpdateData(false);
    }
    //这个包含了弹出对话框得到路径,并将文本显示到EditIn里。如果楼主要手动输入的话,就直接给Pathname赋值好了,前面的删!还有EditIn.SetWindowText(str)可以改为GetIterm(控件ID)->SetWindowText(str);这样更方便,不用关联变量
      

  3.   

    void CTestCodeDlg::OnBnClickedButton1()//点击按钮后
    {
      CStdioFile stdfile;
      CString strText;
      CString strLine;
      stdfile.Open(L"D://Test//data.txt",CFile::modeReadWrite);
      while (stdfile.ReadString(strLine))
      {
       trText+=strLine;
      }
      m_edit.SetWindowText(strText);
    }
      

  4.   

    stdfile.Open(L"D://Test//data.txt",CFile::modeReadWrite);
    这句总是出错
    错误是  stdfile.Open(L"D://Test//data.txt",CFile::modeReadWrite);
    请教原因