|
-
August 17th, 1999, 10:36 PM
#1
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!
-
August 18th, 1999, 03:43 PM
#2
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]
-
August 18th, 1999, 05:54 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|