A simple but detailed explanation of the Windows file map reading data file example

Source: Internet
Author: User

#include <windows.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace Std;

int main (int argc, char *argv[])
{
Step 1 Open the file File_flag_write_through
HANDLE hfile = CreateFile (
"Demo.txt",
Generic_write | generic_read,//if you want to map a file: This must be set to read-only (Generic_read) or read-write
0,//This is set to open file any attempt will fail
Null
Open_existing,
File_attribute_normal,//| File_flag_write_through, "solution 1"
NULL);
if (hfile! = INVALID_HANDLE_VALUE)//File Open failed return handle IS-1
This step must be tested, see step 2 in detail
{
cout<< "file opened successfully ~!\n";
}
Else
{
cout<< "File open failed! \ n ";
DWORD d = GetLastError ();
cout<<d<<endl;
return-1;
}

Step 2 Create a memory-mapped file
DWORD dwfilesize = GetFileSize (hfile, NULL);
printf ("File Size:%d\n", dwfilesize);
HANDLE Hfilemap = createfilemapping (
hfile,//If this value is Invalid_handle_value, it is legal, you must test it on the step.
NULL,//default security
Page_readwrite,//readable/writable
0,//2 x 32 digits for a 64-digit number, maximum file bytes,
High byte, when the file size is less than 4G, the high byte is always 0
0,//dwfilesize,//This is a low byte, which is the main parameter, if 0, take the file true size
NULL);
if (hfilemap! = NULL)
{
cout<< "Memory mapped file creation succeeded ~!\n";
}
Else
{
cout<< "Memory-mapped file creation failed!" "<<endl;
}

Step 3: Map the file data to the address space of the process
PVOID pvfile = MapViewOfFile (//pvfile is the obtained pointer, use it to manipulate the file directly
Hfilemap,
File_map_write,//writable
0,//file pointer header position high byte
0,//file pointer header position low byte must be the integer of the allocation granularity, and the granularity of Windows is 64K
0); The end of the file to be mapped, if 0, from the needle to the end of the real file
if (pvfile! = NULL)
{
cout<< "File data mapped to the address of the process successfully ~!\n";
}
Else
{
cout<< "File data mapped to the address of the process successfully ~!\n";
}

Step 4: Manipulate the file as you would any memory, and demonstrate the ability to reverse the entire file
Char *p = (char*) pvfile;
printf ("%s\n", p);
for (unsigned int i = 0; I <= dwfilesize/2; i++)
{
int ntmp = P[i];
P[i] = p[dwfilesize-1-i];
P[dwfilesize-1-I] = ntmp;
}
printf ("%s\n", p);

cout<< "Swap complete, click Enter to exit" <<endl;

Step 5: Related release work
UnmapViewOfFile (Pvfile); Release the head pointer of the memory-mapped file
CloseHandle (HFILEMAP); Memory-mapped file handle
CloseHandle (hfile); Close File
GetChar ();
return 0;
}

A simple but detailed explanation of the Windows file map reading data file example

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.