MFC CopyFile 使用示例
时间: 2023-07-11 21:37:19 浏览: 54
下面是一个 MFC CopyFile 使用示例:
```cpp
BOOL CopyFile(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists)
{
CFile srcFile, destFile;
if (!srcFile.Open(lpExistingFileName, CFile::modeRead))
{
return FALSE;
}
if (destFile.Open(lpNewFileName, bFailIfExists ? CFile::modeCreate | CFile::modeWrite : CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate))
{
destFile.SeekToEnd();
}
else
{
return FALSE;
}
const int BUFFER_SIZE = 4096;
char buffer[BUFFER_SIZE];
UINT nBytesRead = 0;
while ((nBytesRead = srcFile.Read(buffer, BUFFER_SIZE)) > 0)
{
destFile.Write(buffer, nBytesRead);
}
srcFile.Close();
destFile.Close();
return TRUE;
}
```
该函数可以将一个文件从一个位置复制到另一个位置。要使用该函数,请调用以下代码:
```cpp
BOOL bSuccess = CopyFile(_T("C:\\Temp\\SourceFile.txt"), _T("C:\\Temp\\DestinationFile.txt"), FALSE);
if (bSuccess)
{
// 成功复制文件
}
else
{
// 复制文件失败
}
```
其中,第一个参数是源文件的路径,第二个参数是目标文件的路径,第三个参数指定在目标文件已经存在的情况下是否覆盖目标文件。如果第三个参数为 TRUE,则在目标文件已经存在时不会复制文件。如果第三个参数为 FALSE,则在目标文件已经存在时会覆盖目标文件。