使用SDI应用程序(MFC)从文本文件读取数据并显示数据

Read a data from a text file and display data using SDI Application (MFC)?

本文关键字:数据 读取 文件 显示 文本 应用程序 SDI MFC 使用      更新时间:2023-10-16

从文件中读取和写入对我来说更容易,但我无法在SDI应用程序(MFC)中完成。。。。我需要一些指导来解决这个问题。。。。。。。。。。这是构造函数

    CFileDoc::CFileDoc()
    {
    // TODO: add one-time construction code here
      CFileDialog m_IdFile(true);
      if(m_IdFile.DoModal()==IDOK)
       m_sFileName= m_IdFile.GetFileName();
       fstream outFile;
       string data;
       outFile.open(m_sFileName,ios::in);
    {
              while(outFile.eof()!=true)
            {
                getline(outFile,data);
                m_sName=data;
          }
}
    outFile.close();

}

这一部分我做错了

  m_sName=data;

因为m_sName的数据类型是CString,而数据的数据类型则是字符串

 CString m_sName;
 string data;

错误是

binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

不能将std::string分配给CString,但CString有一个接受const char*:的构造函数

m_sName = data.c_str();

此外,while(outFile.eof()!=true)是错误的(最后一次读取操作将在设置eof之后完成!),您应该执行:

while (getline(outFile, data))
{
 ...