Click to See Complete Forum and Search --> : Read and write file


YKQ
August 17th, 1999, 10:36 PM
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!

Rail Jon Rogut
August 18th, 1999, 03:43 PM
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/
railro@earthlink.net

Scott Kirkland
August 18th, 1999, 05:54 PM
Try using the flags CFile::shareDenyNone | CFile::modeNoTruncate | CFile::modeRead when you open the file,also check your capitilization in
fillnput1.ReadString(str);