如果断电,mkdir和copyFile不完整

Incomplete mkdir and copyFile if power lost

本文关键字:copyFile 断电 mkdir 如果      更新时间:2023-10-16

我有一个应用程序,它可以创建一个目录并将文件复制到其中。但是,如果窗口正确关闭并且电源丢失,文件将不总是可用或完整的。

我的操作系统是Windows Embedded POSReady7(版本6.1 Build 7601:Service Pack 1),硬盘上的"写缓存"选项已禁用。我有一个C++应用程序。我也使用过刷新(_F),但没有帮助。

我已经写了一个测试应用程序,我可以在那里看到问题。我启动应用程序,在输出完成后等待十秒钟,然后拔下电源。重新启动后,这些文件不可用。

这是我的代码:

#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;
}

您是否尝试过将副本放入范围内?

{
CopyFileEx( _T("D:\temp\test.txt"), _T("D:\temp\FileCopyTest\test.txt"), nullptr, nullptr, FALSE, COPY_FILE_NO_BUFFERING );
}