简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM