打开文件时Boost mapped_file崩溃

时间:2017-11-27 17:47:06

标签: c++ windows boost visual-studio-2015 memory-mapped-files

我使用Boost 1.62将文件映射到内存boost/iostreams/device/mapped_file.hpp

它适用于大多数文件,但某些文件会导致程序崩溃。我使用的是Windows 10 x64系统,文件如NTUSER.DAT.LOG,但我可以使用编辑器打开它们并查看内容。

以下是我用来打开文件的代码:

uint64_t read_directory(char* in_path) {
    struct _stat64 fileinfo;
    const char* filename;
    int result;

    int counter = 0;

    boost::iostreams::mapped_file_source file;

    for (auto it : recursive_directory_range(in_path)) {

        string filename_s = it.path().string();
        filename = filename_s.c_str();
        result = _stat64(filename, &fileinfo);
        // cout << result << endl;

        if (boost::filesystem::is_directory(it) || fileinfo.st_size == 0) {
            continue;
        }
        if (boost::filesystem::is_regular_file(it)) {

            file.open(filename, numberOfBytes);
        }

    if (file.is_open()) {

        int* data = (int*)file.data();
        file.close();
    }

    counter++;
    if (counter % 1000 == 0) {
        cout << counter << endl;
    }
}

递归步行从这里开始: How do you iterate through every file/directory recursively in standard C++?

尝试访问此特定文件后,程序在调试器中崩溃并显示以下消息:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::ios_base::failure> > at memory location 0x0000004529D2F118.

该程序以具有管理员权限的用户身份运行,但这应该不是问题,因为在Notepad ++中打开显示文件内容。我做错了什么?

编辑:我认为当另一个进程正在使用该文件时可能会有一些事情要做。我用gpodder试了一下,当它打开时,我无法映射文件。关闭程序后,我可以再次执行此操作。有没有办法事先检查一下?

0 个答案:

没有答案
相关问题