【问题标题】:Read a data from a text file and display data using SDI Application (MFC)?从文本文件中读取数据并使用 SDI 应用程序 (MFC) 显示数据?
【发布时间】:2014-10-28 09:22:29
【问题描述】:

从文件中读取和写入对我来说更容易,但我无法在 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)

【问题讨论】:

标签: c++ mfc


【解决方案1】:

您不能将std::string 分配给CString,但CString 有一个构造函数采用const char*

m_sName = data.c_str();

另外while(outFile.eof()!=true) 是错误的(最后一次读取操作将在之后 eof 设置完成!),你应该这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多