1

Reading and Writing from the file were easier for me but i am unable to do it in SDI Application (MFC) .... i need some guidance to solve this .......... here is constructor

    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();

}

i am doing mistake in this part

  m_sName=data;

because the Data type of m_sName is CString and the data type of data is string

 CString m_sName;

 string data;

the error is

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)
Khan
  • 35
  • 6
  • 1
    Relevant: that isn't your only mistake. [**Read this**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). And you'll probably find [**this important as well.**](http://en.cppreference.com/w/cpp/string/basic_string/c_str). – WhozCraig Sep 03 '14 at 19:00

1 Answers1

1

You can't assign a std::string to a CString, but CString has a constructor taking a const char* :

m_sName = data.c_str();

Also while(outFile.eof()!=true) is wrong (the last read operation will be done after eof is set !), you should do :

while (getline(outFile, data))
{
 ...
quantdev
  • 22,595
  • 5
  • 47
  • 84