简体   繁体   English

Windows C:无法创建文件的Mmap

[英]windows c : failed to create mmap of a file

I need to create mmap of a file. 我需要创建文件的mmap。 Since windows does not support mmap, i tried MapViewOfFile() method. 由于Windows不支持mmap,因此我尝试了MapViewOfFile()方法。 But this fails. 但这失败了。

Here's my code : 这是我的代码:

char template[1024];
snprintf(template, sizeof(template) / sizeof(char), "%s", "C:\\Users\\Ijas\\Downloads\\ijas.txt");

HANDLE  hfile = CreateFile(template, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);

if (hfile == INVALID_HANDLE_VALUE)
{
    fprintf(stderr, "CreateFile() error 0x%08x \n", GetLastError());
    return NULL;
}

HANDLE map_handle = CreateFileMapping(hfile, NULL, PAGE_READWRITE | SEC_RESERVE, 0, 0, 0);
if (map_handle == NULL)
{
    fprintf(stderr, "CreateFileMapping() error 0x%08x\n", GetLastError());
    CloseHandle(hfile);
    return NULL;

}

sp = (char*)MapViewOfFile(map_handle, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0);

if (sp->buffer == NULL)
{
    fprintf(stderr, "MapViewOfFile() error 0x%08x\n", GetLastError());
    CloseHandle(hMapFile);
    CloseHandle(hfile);
    return NULL;
}

Output : 输出:

CreateFile() error 0x00000003

Hint : template is an existing file. 提示:模板是现有文件。

Anything wrong in my code? 我的代码有什么问题吗? Please help me out? 请帮帮我吗?

As discussed in comments, with various possibilities of try-and-hit, the problem seems to be with Unicode and Ansi . 正如评论中所讨论的那样,通过各种尝试尝试,问题似乎出在UnicodeAnsi template is declared as char* but is passed to CreateFile which by default calls CreateFileW . template被声明为char*但是被传递给CreateFile ,默认情况下它调用CreateFileW If OP would have compiled the code with C++ compiler, the compiler would have complained about this but with C compiler, it just relied on the coder's intellect. 如果OP用C ++编译器编译代码,则编译器会对此抱怨,但使用C编译器时,它只是依靠编码器的知识。

CreateFileA expects const char* whereas CreateFileW expects const wchar_t* CreateFileA需要const char*CreateFileW需要const wchar_t*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM