CFile复制方法修复文件访问被拒绝的问题

时间:2012-04-25 02:52:13

标签: c++ mfc

我的应用遇到了ramdon错误。生成新文件且其大小为0K时,txt文件访问被拒绝。

我只想复制现有文件,并在关闭当前文件后重命名。

有人能给我一个复制文件的方法吗?感谢。

我的代码段如下。

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());
        }

我认为我的应用程序错误是由上面的代码引起的。

2 个答案:

答案 0 :(得分:1)

CFile没有复制文件的方法。

使用:: CopyFile()函数只需复制文件而无需打开和关闭文件。

答案 1 :(得分:0)

是CFile没有复制文件的方法。您可以使用之前发布的:: CopyFile Win32方法,或者使用另一种(稍微复杂的)方法使用CFile查看CFile::Open方法的示例