Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query in file copying using memory mapped files

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
Hi,
i need some help in implementing the memory mapped file on the same machine. My aim is to copy one file from the machine drive to another file [which is to be created before copying] on the drive of the same machine using memory mapped files.

The steps are as follows:-
1. I have created the memory mapped file object associated with some file on the disk say "D:\\Source.txt". The whole of the file is mapped here. The call for creating the mapping object was successful. This memory mapped object is a named object.
2. Then i tried using MapViewOfFile() for some file operations. These operations were also successful.
3. Now i want to copy the contents of the file "D:\\Source.txt" to the new file "D:\\Destination.txt" on the same drive using the memory mapped object [Use of existing memory mapped object]. The new file is to be created before copying the contents.

Note: I am trying to do this with the help of memory mapped files only.

Can anybody suggest
Code:
 on this?


Thanks in advance
Sanjay
 
Sanjay,

Hope this helps. I tested it and it worked.

Code:
	// Create a memory mapped file
	HANDLE hMappedMemoryFile;
	hMappedMemoryFile = CreateFileMapping((void*)
                                             0xffffffff,
                                             NULL,
                                             PAGE_READWRITE,
                                             0x00,
                                             0xffff,
                                             "MyMappedFile");

	// Map view of file
	LPVOID lpMapView;
	lpMapView = MapViewOfFile(hMappedMemoryFile,
                                  FILE_MAP_READ | FILE_MAP_WRITE,
                                  0, 0,0);

	
	// Open disk file with read access
	CString sReadFileName = "c:\\temp\\readtext.txt";
	HANDLE hReadFile;
	hReadFile = CreateFile(sReadFileName,
                               GENERIC_READ,
                               0,
                               NULL,
                               OPEN_EXISTING,
                               FILE_ATTRIBUTE_NORMAL,
                               NULL);

	// Read from disk file
	BOOL bReadSuccess;
	unsigned long count;
	bReadSuccess = ReadFile(hReadFile,
                                lpMapView,
                                0xffff,
                                &count,
                                0);
	
	// Open disk file with write access
	CString sWriteFileName = "c:\\temp\\writetext.txt";
	HANDLE hWriteFile;
	hWriteFile = CreateFile(sWriteFileName,
                                GENERIC_WRITE,
                                0,
                                NULL,
                                CREATE_ALWAYS,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);

	// Write to disk file
	BOOL bWriteSuccess;
	unsigned long written;
	bWriteSuccess = WriteFile(hWriteFile,
                                  lpMapView,
                                  count,
                                  &written,
                                  0);
	
	Sleep(1000);

	// Clean up.
	// Unmap views and close handles
	UnmapViewOfFile(lpMapView);
	CloseHandle(hMappedMemoryFile);
	CloseHandle(hReadFile);
	CloseHandle(hWriteFile);

I assumed that the write file would always be created. If not just modify the write files CreateFile dwCreationDisposition parameter to your liking.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top