Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №6 / 6 / lab6
.cpp#include "stdafx.h"
using namespace std;
void create();
void map();
void unmap();
char Filename1[_MAX_FNAME];
HANDLE hFile, hMapFile;
LPVOID lpMapAddress;
int a;
int _tmain(int argc, _TCHAR* argv[])
{
system("cls");
cout<<"\n The sixth laboratory work "<<endl;
cout<<" File mapping"<<endl;
getch();
do
{
system("cls");
cout<<"\n Menu\n"<<endl;
cout<<" 1.Creating a file mapping object for the specified file"<<endl;
cout<<" 2.Mapping a view of a file into the address space of the calling process"<<endl;
cout<<" 3.Unmapping a mapped view of a file from the calling process's address space"<<endl;
cout<<" 4.Exit"<<endl;
do
{
cout<<"\n Vibirite punkt menu:"<<endl;
cin>>a;
if(a<0||a>4)
{
cout<<"\n Vi dopustili oshibku!!!(1-4)"<<endl;
getch();
}
}
while(a<0||a>4);
switch(a)
{
case 1:
{
create();
break;
}
case 2:
{
map();
break;
}
case 3:
{
unmap();
break;
}
}
}
while(a!=4);
return 0;
}
void create()
{
system("cls");
cout<<"\nEnter the name of the file: \n"<<endl;
cin>>Filename1;
if(hFile = CreateFile(Filename1,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0))
{
if (hFile == INVALID_HANDLE_VALUE)
{
cout<<"\nError!!!(maybe such file has already exists)"<<endl;
getch();
}
else
{
cout<<"File "<<Filename1<<" successfully opened!"<<endl;
getch();
}
}
else
{
cout<<"\nError!!!"<<endl;
getch();
}
hMapFile = CreateFileMapping(hFile,NULL,PAGE_WRITECOPY,0,0,"MyFileMappingObject");
if (hMapFile != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hMapFile);
hMapFile = NULL;
cout<<"Already exists"<<endl;
getch();
}
if (hMapFile == NULL)
{
cout<<"Could not create file mapping object"<<endl;
cout<<"Error #"<<GetLastError()<<endl;
getch();
}
else
{
cout<<"File mapping object successfully created"<<endl;
getch();
}
}
void map(void)
{
system("cls");
lpMapAddress = MapViewOfFile(hMapFile,FILE_MAP_COPY,0,0,0);
if (lpMapAddress == NULL)
{
cout<<"Could not map view of file"<<endl;
getch();
}
else
{
cout<<"View of file was successfully mapped"<<endl;
getch();
}
}
void unmap(void)
{
system("cls");
if(UnmapViewOfFile(lpMapAddress))
{
cout<<"A mapped view of a file was successfully unmapped "<<endl;
getch();
}
else
{
cout<<"Error!!!"<<endl;
getch();
}
CloseHandle(hMapFile);
CloseHandle(hFile);
}