Memory Map Modify Large file

Source: Internet
Author: User

This article describes using memory-mapped files to modify large files: Adding a piece of data before large file memory, to use a memory-mapped file, you must perform the following steps:

Creates or opens a file kernel object that identifies the file on disk that you want to use as a memory-mapped file;

Create a file map kernel object that tells the system the size of the file and how you intend to access the file;

Let the system map all or part of the file mapping object into your process address space;

When you are finished using the memory-mapped file, you must perform the following steps to clear it:

Tell the system to undo the image of the kernel object from the address space of your process;

Closes the file mapping kernel object;

Closes the file kernel object;

These steps are described in detail below with an example (the purpose of this example is to add some content to file B in front of the content of a file a), which I think you will encounter in program development.

First, we open about a file kernel object, and create a kernel object about B file

To create or open a file kernel object, always call the CreateFile function:

HANDLE CreateFile(
PCSTR pszFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
PSECURITY_ATTRIBUTES psa,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);

The CreateFile function has several parameters, focusing only on the first 3 parameters, namely pszfilename,dwdesiredaccess and dwShareMode.

As you might guess, the first parameter pszfilename is used to indicate the name of the file to create or open (including an option path), and the second parameter dwdesiredaccess to set how to access the file's contents, one of the 4 values listed in the following table can be set.

Value Meaning
0 Can not read or write the contents of the file, when only want to get the properties of the file, please set 0
Generic_read Can read data from a file
Generic_write You can write data to a file
generic_read| Generic_write You can read data from a file or write data to a file

When you create or open a file, when you use it as a memory-mapped file, select the most meaningful one or more access flags to illustrate how you intend to access the file's data, and for memory-mapped files, you must open files for read-only access or read-write access, so you can set generic_ READ or generic_read| Generic_write,

The third parameter dwShareMode tells the system how you want to share the file, and you can set one of the 4 values listed in the following table for dwShareMode:

Value Meaning
0 Any attempt to open the file will fail
File_share_read Other attempts to open a file using Generic_write will fail
File_share_write Other attempts to open a file using Generic_read will fail
File_share_readfile_share_write Other attempts to open the file will be successful

If the CreateFile function succeeds in creating or opening the specified file, it returns a handle to the file kernel object, otherwise returns INVALID_HANDLE_VALUE.

Note that most Windows functions that can return a handle will return NULL if the operation fails, but the CreateFile function will return INVALID_HANDLE_VALUE, which is defined as ((HANDLE)-1),

HANDLEhFile=CreateFile(".\\first.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
HANDLEhmyfile=CreateFile("E:\\my.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.