简体   繁体   English

从 CFile 中读取 CByteArray

[英]Read CByteArray from CFile

I'm writring CByteArray to file:我正在将 CByteArray 写入文件:

    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?现在如何从文件中读取 CByteArray?

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 *'错误 C2664:“CFile::Read”:无法将参数 1 从“CByteArray”转换为“void *”

to complete question with full functional code: (tested under Visual studio 2017)用全功能代码完成问题:(在 Visual Studio 2017 下测试)

BOOL ReadBinaryFile(TCHAR * filename) {

    CFileStatus filestatus;
    BOOL ok = CFile::GetStatus(filename, filestatus);
    if (!ok)
        return FALSE;

    CFile myFile;
    ok = myFile.Open(filename, CFile::modeRead | CFile::typeBinary | CFile::modeNoTruncate);
    if (!ok)
        return FALSE;

    INT_PTR SIZE = (INT_PTR) filestatus.m_size;

    // 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(SIZE); // ensure that buffer is allocated and the size we want it
    UINT bytesRead = myFile.Read(buffer.GetData(), SIZE);

    myFile.Close();
    return TRUE;
}

Looking at thedocumentation for CFile::Read , we see that it takes two parameters: 查看CFile::Read文档,我们看到它需要两个参数:

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 . lpBuf不是CByteArray类型。 It is void* .它是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 ,我们需要通过CByteArray分配一个缓冲区获取指向我们可以用于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))

TheCFile::modeCreate will cause the file to be truncated to 0 bytes. CFile::modeCreate将导致文件被截断为 0 字节。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM