Reputation: 1971
My app met a ramdon error. the txt file access denied when the new file generated and it's size is 0K.
I just want to copy the existing file and rename it after closed the current one.
Could anyone give me a method to Copy a file? Thanks.
my code snippet as below.
ofstream m_LogFile;
CFile logcfile;
if(dwLength > 1024*1024*10 )
{
string fileDate = status.m_mtime.Format("%Y%m%d%H%M%S");
string modulePath = Util::GetModulePath();
string fileNewName(modulePath);
fileNewName += "mytextlog" + fileDate + ".txt";
m_LogFile.close();
// I want to insert CFile copy method before rename it.
logcfile.Rename(m_sLogFileName.c_str(), fileNewName.c_str());
m_LogFile.open(m_sLogFileName.c_str(), ios::out | ios::app);
_findfile(modulePath.c_str());
}
I assumed that my app error caused by the code above.
Upvotes: 0
Views: 990
Reputation: 2848
Yes CFile does not have a method to copy file. You could use ::CopyFile Win32 method as posted before, or there is another (little complicated) way with CFile see example at CFile::Open method
Upvotes: 0
Reputation: 6849
CFile does not have a method to copy a file.
Use the ::CopyFile() function to simply copy a file without any need to open and close the file.
Upvotes: 1