读文件,用CFile::Read整块整块读(比如每次100字节),还要有CFileException的
最好成绩把读出的文件格式为("0x%02X"),谢谢~~

解决方案 »

  1.   

    CFile file;
    CFileException exp;

    if(file.Open("c:\\data.dat",CFile::modeRead|CFile::typeBinary|CFile::shareDenyNone,&exp) == FALSE)
    {
    exp.ReportError(MB_OK|MB_ICONERROR);
    //也可以用exp.GetErrorMessage()获得错误信息自己处理
    return;
    }

    void *pBuffer = new BYTE[100];
    int nRead;
    do {
    nRead = file.Read(pBuffer,100);
    //对pBuffer进行处理
    } while(nRead == 100); delete []pBuffer;
    pBuffer = NULL;
      

  2.   

    至于格式化,你可以把pBuffer转成BYTE*指针,一个字节一个字节的格式化,然后把结果字符串相加,或者用下面的方法快速处理,结果在strResult中.
                      CString strResult;
    CFile file;
    if(file.Open(sPath,CFile::modeRead|CFile::typeBinary))
    {
    BYTE *buf = new BYTE[1024];
    UINT nRead; do
    {
    nRead = file.Read(buf,1024);
    if(nRead > 0)
    {
    char *pTmp = new char[nRead*2+1];
    int nIndex = 0;

    CString sTmp;
    for(UINT i=0; i<nRead; i++)
    {
    BYTE n1 = buf[i]/16;
    BYTE n2 = buf[i]%16; pTmp[nIndex++] = (char)(n1 + (n1<=9? '0':('A'-10)));
    pTmp[nIndex++] = (char)(n2 + (n2<=9? '0':('A'-10)));
    }

    pTmp[nIndex] = '\0';
    strResult += pTmp;
    delete []pTmp;
    }
    }while(nRead == 1024);

    file.Close(); delete []buf;
    }