首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Windows XP上的ReadFile函数失败,错误代码为2

Windows XP上的ReadFile函数失败,错误代码为2
EN

Stack Overflow用户
提问于 2011-03-30 03:23:14
回答 2查看 1.4K关注 0票数 2

我有一个项目的一部分,打算在循环中重新读取文件的某一部分(预先将指针移动到文件的开头)。开始时,代码打开文件并正确地写入“最小数据”,但进一步读取(在相同的文件句柄(!)上)失败,错误代码为2(“找不到文件”)。

这里是与流程相关的代码部分:

virtual-mem-buffer.h:

代码语言:javascript
运行
复制
/**
 * Allocates and manages page-aligned virtual memory of the given amount
 */
class VirtualMemBuffer {
public:
    explicit VirtualMemBuffer(size_t size) { /* skipped */ };
/* skipped */
protected:
    void * data;
public:
    const void * buff() const { return this->data; };
};

header-file.h:

代码语言:javascript
运行
复制
static const size_t cFileSize = 4096;

typedef std::map<std::wstring, HANDLE> handlers_conrainer_type;
typedef std::pair<std::wstring, bool> item_type;

class Config {
public:
    typedef std::vector<item_type > container_type;

    container_type files;
    /* skipped */
};

code-file.cpp (在某个函数中):

代码语言:javascript
运行
复制
VirtualMemBuffer buffer(cFileSize);

Config config(...);
config->files.push_back(item_type(L"C:\\lock-file.lock", true));

/* skipped */

for (Config::container_type::const_iterator it = config->files.begin(); 
     it != config->files.end(); 
     ++it)
{
    HANDLE hFile = CreateFile(
        (LPCWSTR)(it->first.c_str()),
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    if (hFile == INVALID_HANDLE_VALUE) {
        DWORD error = GetLastError();
        // Could not open the file, ignore it
        continue;
    } else {
        DWORD bytes_written = 0;
        BOOL write_ok = WriteFile(
                                    hFile, 
                                    buffer.buff(), 
                                    cFileSize, 
                                    &bytes_written, 
                                    NULL);

        if (!write_ok || (bytes_written != (DWORD)(cFileSize))) {
            // Could not initialize the file, skip the file
            CloseHandle(hFile);
            continue;
        };

        handlers_container.insert(
                std::pair<std::wstring, HANDLE>(it->first, hFile)
                );
    };
};

/* skipped */

for (handlers_conrainer_type::const_iterator it = handlers_container.begin(); 
     it != handlers_container.end(); 
     ++it)
{
    DWORD bytes_read = 0;
    LARGE_INTEGER li;
        li.HighPart = 0;
        li.LowPart = 0;
    BOOL move_ok = SetFilePointerEx(it->second, li, NULL, FILE_BEGIN);
    BOOL read_ok = ReadFile(
                                it->second, 
                                buffer.buff(), 
                                cFileSize, 
                                &bytes_read, 
                                NULL);

    if (!read_ok || (bytes_read != cFileSize)) {
        DWORD error = GetLastError();        // error == 2 :-(
        /* skipped */
    };
};

如您所见,SetFilePointerEx()和ReadFile()都在同一个文件句柄上操作。第一个方法(以及CreateFile()和WriteFile() )从未失败,但ReadFile()从未成功过。

有没有人观察到这样的行为,或者至少对此有任何线索?哪里出了问题,如何修复(或避免)?

使用MS Visual C++ 2008速成版在Windows XP SP3上编译的代码

感谢您的宝贵时间和建议!

EN

回答 2

Stack Overflow用户

发布于 2011-03-30 04:51:08

我已经找到了问题的根源-在MS VC++调试器中测试了ReadFile()失败的错误代码,其中(以及何时)变量实际上超出了(块)作用域,因此充满了垃圾。对于这种情况,值2只是编译器的首选项。

我刚刚注意到这一点,并添加了一些进一步的错误检查和绕过它的代码,并发现真正的错误代码是998 (‘对内存位置的无效访问’),它本身来自VirtualMemBuffer类,其中VirtualAlloc()是用PAGE_READONLY标志:-(调用的。

所以,这是我的错,对不起。

感谢所有花时间来帮助我解决这个问题的人。

票数 1
EN

Stack Overflow用户

发布于 2011-03-30 03:57:46

在尝试读取之前,尝试使用FlushFileBuffers(句柄)将写入提交到磁盘

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5477784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档