CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Posts
    160

    Read and write file

    Hello:
    I use VC++6.0 MFC.I want to open and read a file,and then
    open and write to a file like this:
    void CCodeChange1Dlg::ChangeCode()
    {
    CString str,str2;
    CStdioFile Fillnput1(m_InputName,CFile::modeRead);
    CFile fillnput2(m_OutputName,CFile::modeWrite|CFile::modeCreate);
    fillnput1.ReadString(str);
    str2.Format("%s\r\n",str);
    fillnput2.Write((char*)(LPCTSTR)str2,str2.GetLength());
    }
    This is example the file char only one line.But when i test
    the str2 can't read into file.Which is wrong?

    Welcome any advice!
    Thanks!



  2. #2
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: Read and write file

    You need to use CFile::Open() and CFile::Close() to open and close the file respectively.

    void CCodeChange1Dlg::ChangeCode()
    {
    CString str, str2;
    char szBuffer[MAX_LENGTH];

    CStdioFile fSource;
    CFile fDest;
    CFileException e;

    fSource.Open(m_InputName, CFile::modeRead | CFile::shareDenyNone, &e);

    fDest.Open(m_OutputName, CFile::modeWrite | CFile::modeCreate | CFile::shareDenyNone, &e);


    // You should add error checking code to ensure that the files were opened here!

    // Normally you would loop through the source file here

    fSource.ReadString(str);

    str2.Format("%s\r\n",str);

    strcpy(szBuffer, str2);

    fDest.Write(szBuffer, (str2.GetLength() + 1));

    fSource.Close();
    fDest.Close();

    }


    Rail

    ------------
    Recording Engineer/Software Developer
    Rail Jon Rogut Software
    http://home.earthlink.net/~railro/
    [email protected]

  3. #3
    Join Date
    Aug 1999
    Location
    New Hampshire
    Posts
    1

    Re: Read and write file

    Try using the flags CFile::shareDenyNone | CFile::modeNoTruncate | CFile::modeRead when you open the file,also check your capitilization in
    fillnput1.ReadString(str);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured