Click here to Skip to main content
15,879,184 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Encrypting Archives with AES Pin
sarfaraznawaz19-May-11 19:53
sarfaraznawaz19-May-11 19:53 
Questionabout CListCtrl Pin
luyuxibaby19-May-11 0:41
luyuxibaby19-May-11 0:41 
AnswerRe: about CListCtrl Pin
CPallini19-May-11 2:23
mveCPallini19-May-11 2:23 
QuestionMFC Program and Very Large Text Files Pin
Andy20219-May-11 0:37
Andy20219-May-11 0:37 
AnswerRe: MFC Program and Very Large Text Files Pin
Maximilien19-May-11 1:05
Maximilien19-May-11 1:05 
GeneralRe: MFC Program and Very Large Text Files [modified] Pin
federico.strati19-May-11 1:26
federico.strati19-May-11 1:26 
GeneralRe: MFC Program and Very Large Text Files Pin
Andy20219-May-11 22:04
Andy20219-May-11 22:04 
GeneralRe: MFC Program and Very Large Text Files Pin
federico.strati20-May-11 23:25
federico.strati20-May-11 23:25 
Hello Andy,

what follows is extracted from the book from Jeffrey Richter
"Programming Applications for Microsoft Windows"

It is written for a 32bit O.S. so be careful to adapt it in case you work on Win 7

--- start ---

Processing a Big File Using Memory-Mapped Files

In an earlier section, I said I would tell you how to map a 16-EB file into a small address space.
Well, you can't. Instead, you must map a view of the file that contains only a small portion of the file's data.
You should start by mapping a view of the very beginning of the file.

When you've finished accessing the first view of the file, you can unmap it and then map a new view
starting at an offset deeper within the file. You'll need to repeat this process until you access the complete file. This certainly makes dealing with large memory-mapped files less convenient, but fortunately most files are small enough that this problem doesn't usually come up.

Let's look at an example using an 8-GB file and a 32-bit address space.
Here is a routine that counts all the 0 bytes in a binary data file in several steps:

__int64 Count0s(void) {

// Views must always start on a multiple
// of the allocation granularity

SYSTEM_INFO sinf;
GetSystemInfo(&sinf);

// Open the data file.

HANDLE hFile = CreateFile("C:\\HugeFile.Big", GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);

// Create the file-mapping object.
HANDLE hFileMapping = CreateFileMapping(hFile, NULL,
PAGE_READONLY, 0, 0, NULL);
DWORD dwFileSizeHigh;

__int64 qwFileSize = GetFileSize(hFile, &dwFileSizeHigh);
qwFileSize += (((__int64) dwFileSizeHigh) << 32);

// We no longer need access to the file object's handle.
CloseHandle(hFile);

__int64 qwFileOffset = 0, qwNumOf0s = 0;
while (qwFileSize > 0) {

  // Determine the number of bytes to be mapped in this view
  DWORD dwBytesInBlock = sinf.dwAllocationGranularity;
  if (qwFileSize < sinf.dwAllocationGranularity)
    dwBytesInBlock = (DWORD) qwFileSize;
  PBYTE pbFile = (PBYTE) MapViewOfFile(hFileMapping, FILE_MAP_READ,
                            (DWORD) (qwFileOffset >> 32), // Starting byte
                            (DWORD) (qwFileOffset & 0xFFFFFFFF), // in file
                             dwBytesInBlock); // # of bytes to map
  // Count the number of Js in this block.
  for (DWORD dwByte = 0; dwByte < dwBytesInBlock; dwByte++) {
    if (pbFile[dwByte] == 0)
    qwNumOf0s++;
  }
  // Unmap the view; we don't want multiple views
  // in our address space.
  UnmapViewOfFile(pbFile);
  // Skip to the next set of bytes in the file.
  qwFileOffset += dwBytesInBlock;
  qwFileSize -= dwBytesInBlock;
}

CloseHandle(hFileMapping);
return(qwNumOf0s);
}


This algorithm maps views of 64 KB (the allocation granularity size) or less.
Also, remember that MapViewOfFile requires that
the file offset parameters be a multiple of the allocation granularity size.

As each view is mapped into the address space, the
scanning for zeros continues. After each 64-KB chunk of the file has been mapped and scanned,
it's time to tidy up by closing the file-mapping object.

--- end ---

Cheers

Federico
GeneralRe: MFC Program and Very Large Text Files Pin
Andy20225-May-11 21:33
Andy20225-May-11 21:33 
GeneralRe: MFC Program and Very Large Text Files Pin
federico.strati26-May-11 2:14
federico.strati26-May-11 2:14 
AnswerRe: MFC Program and Very Large Text Files Pin
Chandrasekharan P19-May-11 1:22
Chandrasekharan P19-May-11 1:22 
AnswerRe: MFC Program and Very Large Text Files Pin
jschell19-May-11 8:21
jschell19-May-11 8:21 
GeneralRe: MFC Program and Very Large Text Files Pin
Andy20219-May-11 22:10
Andy20219-May-11 22:10 
GeneralRe: MFC Program and Very Large Text Files Pin
jschell20-May-11 10:13
jschell20-May-11 10:13 
QuestionEmbedding Excel in a dialog Pin
efraimyy18-May-11 22:49
efraimyy18-May-11 22:49 
AnswerRe: Embedding Excel in a dialog Pin
Chandrasekharan P19-May-11 1:35
Chandrasekharan P19-May-11 1:35 
GeneralRe: Embedding Excel in a dialog Pin
efraimyy19-May-11 2:58
efraimyy19-May-11 2:58 
GeneralRe: Embedding Excel in a dialog Pin
efraimyy19-May-11 3:00
efraimyy19-May-11 3:00 
GeneralRe: Embedding Excel in a dialog Pin
Chandrasekharan P19-May-11 22:04
Chandrasekharan P19-May-11 22:04 
GeneralRe: Embedding Excel in a dialog Pin
efraimyy21-May-11 21:40
efraimyy21-May-11 21:40 
GeneralRe: Embedding Excel in a dialog Pin
Chandrasekharan P22-May-11 18:42
Chandrasekharan P22-May-11 18:42 
QuestionAdding Scrollbar to a Dialog in MFC Pin
pix_programmer18-May-11 20:52
pix_programmer18-May-11 20:52 
AnswerRe: Adding Scrollbar to a Dialog in MFC Pin
Chandrasekharan P18-May-11 22:04
Chandrasekharan P18-May-11 22:04 
GeneralRe: Adding Scrollbar to a Dialog in MFC Pin
pix_programmer18-May-11 22:12
pix_programmer18-May-11 22:12 
GeneralRe: Adding Scrollbar to a Dialog in MFC Pin
Chandrasekharan P18-May-11 22:26
Chandrasekharan P18-May-11 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.