I'm writring CByteArray to file:
CFile myFile;
CByteArray m_baToques;
if(myFile.Open(_T(file), CFile::modeReadWrite | CFile::modeCreate))
{
myFile.Write(m_baToques.GetData(),m_baToques.GetSize());
myFile.Write(m_baDst.GetData(), m_baDst.GetSize());
myFile.Write(m_baFeriados.GetData(), m_baFeriados.GetSize());
}
Now How to read CByteArray from file?
I try:
CFile myFile;
if(myFile.Open(_T(file), CFile::modeReadWrite | CFile::modeCreate))
{
myFile.Read(m_baToques,m_baToques.GetSize());
myFile.Read(m_baDst, m_baDst.GetSize());
myFile.Read(m_baFeriados, m_baFeriados.GetSize());
}
error C2664: 'CFile::Read' : cannot convert parameter 1 from 'CByteArray' to 'void *'
Looking at the documentation for CFile::Read
, we see that it takes two parameters:
virtual UINT CFile::Read(void* lpBuf, UINT nCount);
lpBuf
Pointer to the user-supplied buffer that is to receive the data read from the file.
nCount
The maximum number of bytes to be read from the file. For text-mode files, carriage return–linefeed pairs are counted as single characters.
lpBuf is not of type CByteArray
. It is void*
. Thus the compiler error.
To read into a CByteArray
, we need to allocate a buffer via CByteArray
and get a pointer to said buffer that we can use for lpBuf:
CByteArray buffer;
buffer.SetSize(1024); // ensure that buffer is allocated and the size we want it
UINT bytesRead = myFile.Read(buffer.GetData(), buffer.GetSize());
// use bytesRead value and data now in buffer as needed
Note, that in your question, you have the following line on the read path:
if(myFile.Open(_T(file), CFile::modeReadWrite | CFile::modeCreate))
The CFile::modeCreate
will cause the file to be truncated to 0 bytes. There won't be anything to read.
Did you mean to write something more like this?
if(myFile.Open(_T(file), CFile::modeRead | CFile::typeBinary | CFile::modeNoTruncate))