EVC memory and file Interaction

Source: Internet
Author: User

// ========================================== =======================================================< 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 >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.