Home > Software engineering >  Create a mapping file memory map view after how to read binary file??????? For help
Create a mapping file memory map view after how to read binary file??????? For help

Time:10-08

Have created the map view, the following:

//create the file object
HANDLE hFile=CreateFile (
SFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL.
NULL);
If (hFile==INVALID_HANDLE_VALUE)
{
return ;
}

//create a file mapping object
HANDLE hFileMap=CreateFileMapping (PAGE_READONLY hFile, NULL, 0, 0, NULL);
If (hFileMap==NULL)
{
return;
}
//get the system allocation granularity
SYSTEM_INFO SysInfo.
GetSystemInfo (& amp; The SysInfo);
DWORD dwGran=SysInfo. DwAllocationGranularity;

//get the file size
DWORD dwFileSizeHigh;
__int64 qwFileSize=GetFileSize (hFile, & amp; DwFileSizeHigh);
QwFileSize |=(((__int64) dwFileSizeHigh) & lt; <32);

//offset
__int64 qwFileOffset=0;

//the block size
DWORD dwBlockBytes=0;
For (int I=1; ; + + I)
{
If (qwFileSize & lt;=I * dwGran)
{
DwBlockBytes=I * dwGran;
break;
}
}
If (qwFileOffset & gt;=0)
{
//map view
//if the call MapViewOfFile (), if dwNumberOfBytesToMap is greater than the file size, then the call will fail,
//and the error code is access denied, so the last parameter MapViewOfFile cannot use dwBlockSize and can only use qwFileSize,
//MapViewOfFile return values can't be a TCHAR *, for UNICODE interpreted as wide character, will go wrong, can only use char *
Char * lpbMapAddress=(char *) MapViewOfFile (hFileMap FILE_MAP_READ, 0, 0, qwFileSize);
If (lpbMapAddress==NULL)
{
The CloseHandle (hFileMap);
The CloseHandle (hFile);
return;
}

///read the file content

}


To ask you, how specific to read the contents of the file, binary file, content contains a double, int type data such as???????


CodePudding user response:

LpbMapAddress is the file content, similar to a memory data
But the binary file only if you understand the file structure (such as structure), otherwise it's just a piece of data

CodePudding user response:

Binary read directly to the array and the need to parse corresponding field back to other data

CodePudding user response:

You don't have a char * lpbMapAddress?
Is this the file content?

CodePudding user response:

refer to the second floor oyljerry response:
binary read directly to the array and the need to parse corresponding field back to other data



I'm not very understand about this, suppose I binary file is the actual content of hello! Front is 3.14 2.45, character types, behind two type is float, how should read???
Here is my written before a read the code, trouble for help to see see!! Thank you
Char header;
Do
{
The header=(char) lpbMapAddress;
LpbMapAddress++;
} while (header!='! ');

LpbMapAddress float * ax=(float *);
Float * ay=(double *) lpbMapAddress + 4;

CodePudding user response:

Fyi:
 void HexDump) (char * buf, int len, int addr) {
Int I, j, k;
Char binstr [80].

For (I=0; iIf (0==16) (I %) {
Sprintf (binstr, "% x - 08", I + addr);
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
} else if (15==16) (I %) {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
Sprintf (binstr, "% s", binstr);
For (j=I - 15; J<=I; J++) {
Sprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
} else {
Sprintf (binstr, "% s % 02 x", binstr, buf (unsigned char) [I]);
}
}
If (0! 16)=(I %) {
16 k=16 - (I %);
For (j=0; JSprintf (binstr, "% s", binstr);
}
Sprintf (binstr, "% s", binstr);
K=16 - k;
For (j=I - k; JSprintf (binstr, "% s % c," binstr, ('! '& lt; Buf [j] & amp; & Buf [j] <='~')? Buf [j] : '. ');
}
Printf (" % s \ n ", binstr);
}
}

CodePudding user response:

reference 4 floor qyt_1100203049 response:
Quote: refer to the second floor oyljerry response:

Binary read directly to the array and the need to parse corresponding field back to other data



I'm not very understand about this, suppose I binary file is the actual content of hello! Front is 3.14 2.45, character types, behind two type is float, how should read???
Here is my written before a read the code, trouble for help to see see!! Thank you
Char header;
Do
{
The header=(char) lpbMapAddress;
LpbMapAddress++;
} while (header!='! ');

LpbMapAddress float * ax=(float *);
Float * ay=(double *) lpbMapAddress + 4;


You actually this problem has become the binary memory parsing problem, has nothing to do with memory mapping, you according to your structure data parsing rules,
Behind the two type is float,
Float * ay=(double *) lpbMapAddress + 4;
This sentence to
Float * ay=(float *) (lpbMapAddress + 4);

Don't consider unicode, parsing success again,
  • Related