Nano HE
Nano HE

Reputation: 1971

CFile Copy Method to fix file access denied issue

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

Answers (2)

Paskas
Paskas

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

Gautam Jain
Gautam Jain

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

Related Questions