C ++ uses memory file ing to read and write files

Source: Internet
Author: User
C ++ uses memory file ing to read and write files

I need to read and write 2 TB of data each time recently, so I found a solution. I hope you will be familiar with reading files. It can be used one day.

 

Handle m_handle_file = createfile (strfilepath, generic_read | generic_write, 0, null, open_existing, file_attribute_normal, null );
If (m_handle_file = invalid_handle_value)
{
Trace ("% d \ r \ n", getlasterror ());
Closehandle (m_handle_file );
Return;
}
// Create a file ing object
Handle m_handle_map = createfilemapping (m_handle_file, null, page_readwrite, 0, 0, null );
If (m_handle_map = NULL)
{
Trace ("% d \ r \ n", getlasterror ());
Closehandle (m_handle_map );
Return;
}

// Obtain the granularity allocated by the System
System_info m_systeminfo;
Getsysteminfo (& m_systeminfo );
DWORD dwgran = m_systeminfo.dwallocationgranularity;
DWORD dwfilesizehigh;

_ Int64 qwfilesize = getfilesize (m_handle_file, & dwfilesizehigh );
Qwfilesize | = (_ int64) dwfilesizehigh) <32 );
Closehandle (m_handle_file );
// Offset address
_ Int64 qwfileofset = 0;
DWORD dwblocbyte = dwgran;
While (qwfilesize> 0)
{
If (qwfilesize <dwblocbyte)
{
Dwblocbyte = (DWORD) qwfilesize; // the actual size if it is smaller
}
// Ing View
Lpbyte lpmapaddress = (lpbyte) mapviewoffile (m_handle_map, latency, (DWORD) (qwfileofset> 32), (DWORD) (qwfileofset & 0 xffffffffff), dwblocbyte );
If (lpmapaddress = NULL)
{
Trace ("% d \ r \ n", getlasterror ());
Closehandle (m_handle_map );
Return;
}
// Access the ing View
/* For (DWORD I = 0; I <dwblocbyte; I ++)
{
Byte temp = * (lpmapaddress + I );
}*/
// Undo the file Image
Unmapviewoffile (lpmapaddress );
Qwfileofset + = dwblocbyte;
Qwfilesize-= dwblocbyte;
}

Closehandle (m_handle_map );

 

The following functions are used in File Memory ing:
1) createfilemapping
2) flushviewoffile
3) mapviewoffile
4) mapviewoffileex
5) mapviewoffilevlm
6) openfilemapping
7) unmapviewoffile
8) unmapviewoffilevlm
Function details:
1. createfilemapping creates a famous or unknown file image for the specified file;
Handle createfilemapping (
Handle hfile, // the handle of the ing File
Lpsecurity_attributes lpfilemappingattributes,
// Security descriptor pointer
DWORD flprotect, // protection of ing objects
DWORD dwmaximumsizehigh, // the maximum length of the object is 32 bits
DWORD dwmaximumsizelow, // the maximum length of the object is 32 bits
Lptstr lpname // name of the file memory ing object
);
Note:
Hfile: the handle of the ing file. The open mode of the file must be consistent with that specified by the flprotect parameter. If the value of this parameter is 0 xffffffffff, you must specify the size of the ing object in the dwmaximumsizehigh and dwmaximumsizelow parameters. In addition, the file ing object will be created in the replace file on the Virtual Memory Page of the operating system, instead of using the disk file, and the size of the ing object must be given. File ing objects are shared by copies, inheritance, or names.
Lpfilemappingattributes: Security Descriptor pointer, which determines whether the return handle can be inherited by the quilt process. If it is null, the child process cannot be inherited. In WINNT, if it is null, the file ing object gets a default security descriptor.
Flprotect: attempts to specify the protection mode for the obtained file. It can be set to the following values:
Page_readonly: Read-only attribute, and the file corresponding to hfile must be opened in the form of generic_read.
Page_readwrite: readable and writable attributes, and the file corresponding to hfile must be opened in the form of generic_read and generic_write.
Page_writecopy: this operation is performed after the writable area is copied, and the file corresponding to the hfile must be opened in the form of generic_read and generic_write.
Dwmaximumsizehigh, dwmaximumsizelow: if the two parameters are 0, the maximum length of the file ing object is equal to the file length specified by hfile.
Lpname: name of the file ing object. If this name already exists, the ing object will be processed according to the name specified by flprotect. If this parameter is null, a file ing object without a name is created. If the name of this parameter is the same as that of the System Event, the function fails to be executed. getlasterror returns error_invalid_handle;
Returned value: the handle of the file ing object is returned after the function call is successful. If the file ing object already exists, the handle of the original ing object is returned. getlasterror returns error_already_exists. If the function fails to be executed, null is returned.
2. flushviewoffile write all the modifications in the file ing view back to the disk file.
Bool flushviewoffile (
Lpcvoid lpbaseaddress, // start address of the modified content
DWORD dwnumberofbytestoflush // number of modified bytes
);
If the function is successfully executed, a non-zero value is returned.
3. mapviewoffile maps a File View to the address space of the calling Process
Lpvoid mapviewoffile (
Handle hfilemappingobject, // handle of the created file ing object
DWORD dwdesiredaccess, // Access Mode
DWORD dwfileoffsethigh, // 32-bit file offset height
DWORD dwfileoffsetlow, // low 32-bit file offset
DWORD dwnumberofbytestomap // ing View Size
);
Note:
Hfilemappingobject: handle of the file ing object returned by createfilemapping or openfilemapping.
Dwdesiredaccess: Access Mode of the ing view, which is related to the protection mode flprotect of the created file ing object. It can be set to the following values:
File_map_write: A file view with read/write attributes is created. The protection mode is page_readwrite.
File_map_read: A file view with read-only attributes is created. The protection mode is page_readwrite or page_readonly.
File_map_all_access: Same as file_map_write
File_map_copy: When the protection mode is page_writecopy, A View File is obtained. When you write a view file, the page is automatically exchanged, and your modifications do not damage the original data.
Dwnumberofbytestomap: the size of the part of the ing file. If it is 0, the entire file is mapped.
Return Value:
If yes, return the start address of the ing view. If no, return null.
4. mapviewoffileex maps a File View to the address space of the calling process, and allows the calling process to specify a special memory address for the ing view.
Lpvoid mapviewoffileex (
Handle hfilemappingobject, // handle of the file ing object
DWORD dwdesiredaccess, // Access Mode
DWORD dwfileoffsethigh, // 32-bit file offset height
DWORD dwfileoffsetlow, // low 32-bit file offset
DWORD dwnumberofbytestomap, // map the size of the View
Lpvoid lpbaseaddress // specify the actual memory address of the ing View
);
Note:
Same as mapviewoffile, function execution fails if the specified memory address space is not enough.
5. openfilemapping open a named file ing object
Handle openfilemapping (
DWORD dwdesiredaccess, // Access Mode
Bool binherithandle, // inheritance flag
Lptstr lpname // Object Name Pointer mapped to a file
);
Note:
Dwdesiredaccess: the access mode is the same as that in mapviewoffile.
Binherithandle: the inheritance flag, whether it can be inherited and used by a new process. If it is true, it can be inherited by a new process.
Return Value:
A named file ing object is returned. If the ing object fails, null is returned.
6. unmapviewoffile: Delete the ing view of a file
Bool unmapviewoffile (
Lpcvoid lpbaseaddress // start address of the ing View
);
Note:
Lpbaseaddress: the start address of the ing view, which is generated by mapviewoffileex.
Return Value:
If the call is successful, a non-zero value is returned, and all dirty pages in the specified address are written to the hard disk. Zero is returned if the call fails.

Related Article

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.