Windows 系统编程 OpenFile 函数 [英] Windows System Programming OpenFile function

查看:113
本文介绍了Windows 系统编程 OpenFile 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 中 OpenFile 函数的文档位于 这里.我正在尝试这样做:

The documentation for OpenFile function in Windows is located here. And I am trying to do this:

#include "Tchar.h"
#include <windows.h>
int main(int argc, TCHAR *argv[]){

    LPOFSTRUCT _buffer;
    HFILE _hfile_ = OpenFile("E:\\mozunit.txt", _buffer, OF_READ);
    LPVOID _buffer_read;
    LPDWORD _bytes_read;
    bool flag = ReadFile(_buffer, _buffer_read, 5, _bytes_read, NULL);
    CloseHandle(_buffer);
    return 0;
}

现在,当我运行这个时,我得到一个错误,我没有初始化 _buffer.所以为了反驳我这样初始化 _buffer :

Now, when I run this I get an error that I have not initialized the _buffer. So to counter that I initialized _buffer like this:

LPOFSTRUCT _buffer = NULL;

这给了我一个访问冲突错误.这是为什么?

Which gives me an access violation error. Why is that?

推荐答案

根据 文档,第二个参数是...

According to the documentation, the second argument is...

指向 OFSTRUCT 结构的指针,该结构在第一次打开文件时接收有关文件的信息.

A pointer to the OFSTRUCT structure that receives information about a file when it is first opened.

通过将其设置为 NULL,您将尝试使用零地址写入内存.

By setting it to NULL you're attempting to write to memory with the address of zero.

试试这个:

OFSTRUCT buffer;
HFILE hfile = OpenFile("E:\\mozunit.txt", &buffer, OF_READ);
char buffer_read[6];
DWORD bytes_read = 0;
bool flag = ReadFile(hfile, &buffer_read, 5, &bytes_read, NULL);
CloseHandle(hfile);

这篇关于Windows 系统编程 OpenFile 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆