To illustrate the usage of memory ing, the following is an example of personal writing:
First, create a file larger than 2 GB and write a certain amount of test data,
The Code is as follows:
Handle hfile = createfile ("media.mp3", <br/> generic_read | generic_write, <br/> 0, <br/> null, <br/> open_always, <br/> file_attribute_normal, <br/> null); <br/> // todo: change the size of the file. <br/> If (hfile = invalid_handle_value) <br/>{< br/> printf ("failed in create file. /n "); <br/>}< br/> system_info sysinfo; <br/> memset (& sysinfo, 0, sizeof (sysinfo )); <br/> getsysteminfo (& sysinfo); <br/> printf ("sys TEM information/N "); <br/> printf (" process number is: % d/N ", sysinfo. dwnumberofprocessors); // 2 <br/> printf ("allocation granularity is: % d/N", sysinfo. dwallocationgranularity); // 64kb <br/> printf ("page size is: % d/N", sysinfo. dwpagesize); // 4 kb <br/> int retcode = 0; <br/> DWORD distance = 1024*64*1024*10*10; <br/> long highpart = 0; </P> <p> retcode = setfilepointer (hfile, distance, & highpart, file_curre NT); <br/> If (retcode = 0 xffffffff) <br/>{< br/> printf ("error code is: % d", getlasterror ()); <br/>}< br/> char buff [1000]; <br/> memset (buff, 'E', 100); <br/> memset (buff + 100, 'F', 100); <br/> memset (buff + 200, 'G', 100); <br/> memset (buff + 300, 'h ', 100); <br/> memset (buff + 400, 'I', 100); <br/> buff [999] = 0; <br/> DWORD byteswritten = 0; <br/> overlapped owrite; <br/> memset (& owrite, 0, sizeof (owrite); <br/> owrite. off Set = 1024*64*1024*10; <br/> writefile (hfile, buff, 1000, & byteswritten, & owrite ); <br/> printf ("% d bytest has been written! /N ", byteswritten); <br/> closehandle (hfile );
Then read the data in the file and test the Code as follows:
Handle hfile = createfile ("media.mp3", <br/> generic_read | generic_write, <br/> 0, <br/> null, <br/> open_always, <br/> file_attribute_normal, <br/> null); <br/> char buff [1000]; <br/> memset (buff ); <br/> DWORD bytesread = 0; <br/> setfilepointer (hfile, 1, null, file_begin); <br/> overlapped oread; <br/> memset (& oread, 0, sizeof (oread); <br/> oread. offset = 1024*64*1024*10; <br/> readfile (hfile, buff, 1000, & bytesread, & oread ); <br/> printf ("% s/n", buff); <br/> getchar (); <br/> closehandle (hfile );
Finally, we use the memory ing method to flip the strings in this large file and read the strings for testing,
The Code is as follows:
Handle hfile = createfile ("media.mp3", <br/> generic_read | generic_write, <br/> file_1__read | file_1__write, <br/> null, <br/> open_existing, <br/> file_attribute_normal, <br/> null); <br/> If (hfile = invalid_handle_value) <br/>{< br/> printf ("invalid handle value/N"); <br/>}< br/> DWORD filesize = getfilesize (hfile, null ); <br/> printf ("file size is: % f gb/N", (filesize/1024.0)/1024.0)/1024.0); <br /> Handle hfilemap = createfilemapping (hfile, null, page_readwrite, <br/> 0, 0, null); <br/> If (hfilemap = NULL) <br/>{< br/> printf ("Create File mapping object failed. /n "); <br/>}< br/> else <br/> {<br/> DWORD dwnumberofbytestomap = 1024*64*1024*10; <br/> DWORD dwnum = 0; <br/> dword64 offset = 0; <br/> while (filesize) <br/>{< br/> dwnum = dwnumberofbytestomap> filesize? Filesize: dwnumberofbytestomap; </P> <p> lpvoid pvfile = mapviewoffile (hfilemap, file_map_write, Offset> 32, offset & 0 xffffffff, dwnum ); <br/> If (pvfile = NULL) <br/> {<br/> printf ("create map view of file is failed, error code is: % d ", getlasterror (); <br/>}< br/> else <br/>{< br/> filesize-= dwnum; <br/> Offset + = dwnum; <br/> pstr pchansi = (pstr) pvfile; <br/> pchansi [dwnum/sizeof (char)-1] = 0; <br/> _ strrev (pchansi ); <br/> unmapviewoffile (pvfile); <br/>}</P> <p> closehandle (hfilemap ); <br/>}< br/> closehandle (hfile); <br/> handle hfile1 = createfile ("media.mp3", <br/> generic_read | generic_write, <br/> 0, <br/> null, <br/> open_existing, <br/> file_attribute_normal, <br/> null); <br/> overlapped oread; <br/> memset (& oread, 0, sizeof (oread); <br/> oread. offset = 1024*64*1024*10; <br/> char buff [1000]; <br/> DWORD bytesread = 0; <br/> memset (buff ); <br/> readfile (hfile1, buff, 1000, & bytesread, & oread); <br/> printf ("% s/n", buff ); </P> <p> closehandle (hfile1); <br/>
During memory ing, mapviewoffile is used to separate large files and map them to the memory multiple times,
In this way, the error 5 will not be returned because of insufficient memory.
The above describes how to use Windows Memory ing. You must pay attention to the parameter usage during the process,
The above is only one-sided explanation of the problem...