// ========================================== =======================================================< br> // Title:
// EVC memory and file interaction
// Author:
// norains
// Date:
// Saturday 13-may-2006
// ======================== ========================================================== ==========
Using the cfile class of MFC, you can easily read the file data into the memory, or save the memory data as a file.
/*************************************** **********************/
Purpose: Read File data into memory
Szfilepath: Path of the file to be read
Pbasebuf: memory buffer for writing
Dwbuflen: length of the written memory buffer
/*************************************** **********************/
Bool copyfiledatetomem (cstring szfilepath, pbyte & pbasebuf, DWORD & dwbuflen)
{
Cfile mfile (szfilepath, cfile: moderead );
Dwbuflen = mfile. getlength ();
Pbasebuf = (pbyte) realloc (pbasebuf, dwbuflen );
If (pbasebuf = NULL)
Return false;
Mfile. Read (pbasebuf, dwbuflen );
Mfile. Close ();
Return true;
}
/*************************************** **********************/
Purpose: write memory data to a file.
Pbasebuf: Read Memory Buffer
Dwbuflen: Read Memory Buffer Length
Szfilepath: Path of the written file
/*************************************** **********************/
Bool copymemdatetofile (pbyte pbasebuf, DWORD dwbuflen, cstring szfilepath)
{
Cfile mfile (szfilepath, cfile: modewrite | cfile: modecreate );
Mfile. Write (pbasebuf, dwbuflen );
Mfile. Flush ();
Mfile. Close ();
Return true;
}
The call method is also very simple, but note that before calling, you need to initialize the buffer zone:
Bool onlytest ()
{
DWORD m_dwreadbuflen;
DWORD m_dwsavebuflen;
Pbyte m_pbasereadbuf;
Pbyte m_pbasesavebuf;
m_pbasereadbuf = reinterpret_cast (malloc (1);
m_pbasesavebuf = reinterpret_cast (malloc (1 ));
If (copyfiledatetomem (L "test1.txt", m_pbasereadbuf, m_dwreadbuflen)
{< br> return true;
}< br> else
{< br> return false;
}< br>
If (copymemdatetofile (m_pbasesavebuf, m_dwsavebuflen, L "test2.txt")
{< br> return true;
}< br> else
{< br> return false;
}< BR >}