Memory file ing API Test

Source: Internet
Author: User

Address: http://www.cnblogs.com/yejiansnake/archive/2010/11/24/FileMapping.html

Code

1 # include <windows. h>
2
3 void main ()
4 {
5 handle hfile = createfile (
6 "G: \ test.txt", // file name
7 generic_read | generic_write, // read and write the file
8 file_pai_read | file_pai_write,
9 null,
10 create_always, // open an existing file
11 file_attribute_normal,
12 0 );
13
14 handle hmapfile = createfilemapping (
15 hfile,
16 null,
17 page_readwrite, // read and write the ing File
18 2,
19 935*1024*1024 + 64*1024, // these two parameters are 64-bit in total, so the maximum file length supported is 16 EB.
20 null );
21
22 if (hmapfile = invalid_handle_value)
23 {
24 closehandle (hfile );
25 return;
26}
27
28 // map the file data to the address space of the process
29 void * pvfile = mapviewoffile (
30 hmapfile,
31 file_map_read | file_map_write,
32 2,
33 64*1024,
34 0 );
35
36 DWORD dwerror = getlasterror ();
37
38 If (pvfile)
39 {
40 unsigned char * P = (unsigned char *) pvfile;
41
42 p [1] = 52;
43
44 // end
45 unmapviewoffile (pvfile); // undo the ing
46}
47
48 closehandle (hmapfile); // close the file
49 closehandle (hfile); // close the file
50}
51

Conclusion:

1. createfilemapping can open many large files. After the call, the file size is allocated.

2. Two mapviewoffile Parameters
DWORD DwfileoffsethighAnd DWORD
Dwfileoffsetlow
All must be an integer multiple of 64 KB, and the parametersSize_t

DwnumberofbytestomapThe parameter indicates the number of bytes mapped to the memory. The value depends on the actual machine memory size. If the value is 0, the function will map the offset to the space at the end of the file by default, an error occurs when the file size is too large due to insufficient memory.

3. 2 GB memory of the Local Machine. 922 MB has been used, and the maximum ing size is only 935 MB.

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.