我有一个应用程序,可以创建一个目录并将文件复制到其中。正常情况下,这工作正常。但是,如果窗口正确关闭并且断电,文件将不会始终可用或不完整。
我的操作系统是Windows POSReady7 (版本6.1 Build 7601: Service Pack1),并且在硬盘上禁用了“写缓存”选项。我有一个C++应用程序。我也使用了_flushall,但它不起作用。
我已经写了一个测试应用程序,我可以在其中看到问题。我启动应用程序,在输出完成后等待十秒钟,然后拔下电源。文件在重新启动后不可用。
下面是我的代码:
#include "stdafx.h"
#include <iostream>
#include <direct.h>
#include <string>
#include <Windows.h>
#include <atlstr.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string csPathName("D:\\temp\\FileCopyTest\\");
int mkRetValue = _mkdir( csPathName.c_str() );
cout << "Create directory " << csPathName.c_str() << " with return value " << mkRetValue << endl;
BOOL copyRetValue = CopyFileEx( _T("D:\\temp\\test.txt"), _T("D:\\temp\\FileCopyTest\\test.txt"), nullptr, nullptr, FALSE, COPY_FILE_NO_BUFFERING );
cout << "Copy file, return value " << copyRetValue << endl;
int flushRetValue = _flushall();
cout << "flush files: " << flushRetValue << endl;
char c;
cin >> c;
return 0;
}发布于 2015-06-25 22:23:23
你有没有试过把副本放在作用域里?
{
CopyFileEx( _T("D:\\temp\\test.txt"), _T("D:\\temp\\FileCopyTest\\test.txt"), nullptr, nullptr, FALSE, COPY_FILE_NO_BUFFERING );
}https://stackoverflow.com/questions/31052918
复制相似问题