有123.txt文件,用记事本打开后内容是 0 20 10 30 200 30 40 50 60 20 50 30 10 0 0 0 ,等有4000多个数。如何才能循环存储到m_byte[5000]数组中去呢?////定义
BYTE* m_byte;
m_byte = new BYTE[5000];//已经将文件存储到pbuf中
CFile file("123.txt",CFile::modeRead); 
DWORD dwFileLen;
dwFileLen = file.GetLength();
char *pbuf=new char[dwFileLen];
pbuf = new char[dwFileLen+1];
pbuf[dwFileLen] = 0;
file.Read(pbuf,dwFileLen);/////////////接下来改如何循环存储到m_byte[5000]中呢?

解决方案 »

  1.   

    用CStdioFile::ReadString()读取一行,然后利用CString::Tokenize()以空格分隔字符串
      

  2.   

    先用下面函数读取数据到内存CString CUtilityEx::ReadFile(CString strFileName)   
    {    CStdioFile csfFile;   
    CString strFileString  = ""; 
    TRY 
    {
    if(csfFile.Open(strFileName,CFile::modeReadWrite|CFile::shareDenyNone))   
    {  
    CString strTemp;
    while( csfFile.ReadString(strTemp) )
    {
    strFileString += strTemp + " ";

    }  
    csfFile.Close();   
    }  
    }
    CATCH (CMemoryException, e)
    {
    strFileString = "";
    }
    END_CATCH return   strFileString;   
    }
    再用下面函数将文件内容以空格 分隔存储到 bytes数组
    BOOL CUtilityEx::String2Bytes(const char* pszStr, const char* pSeperator, unsigned char* pBuffer, unsigned long& ulBufferLen)
    {
    string strSrc = pszStr;
    string strSep = pSeperator; if (strSrc.length() == 0)
    {
    return FALSE;
    } ulBufferLen = 0;
    size_t nEqualPos = -1;
    size_t nParaStaPos = 0;
    do 
    {
    nEqualPos = strSrc.find(strSep, nParaStaPos);
    if (nEqualPos == string::npos)
    {
    string strLast = strSrc.substr(nParaStaPos, strSrc.length() - nParaStaPos);
    CUtilityEx::RemoveSpace(strLast);
    if(_stscanf(strLast.c_str(), _T("%x"), &pBuffer[ulBufferLen]) == 1)
    {
    ulBufferLen++;
    }
    break;
    }
    string strValue = strSrc.substr(nParaStaPos, nEqualPos - nParaStaPos); nParaStaPos += strValue.length() + 1; CUtilityEx::RemoveSpace(strValue); if(_stscanf(strValue.c_str(), _T("%x"), &pBuffer[ulBufferLen]) == 1)
    {
    ulBufferLen++;
    } } while (nEqualPos != string::npos); return (ulBufferLen > 0);
    }
      

  3.   

    // 用strtok分割字符串
    // 用atoi赋值给BYTEBYTE m_byte[3000];
    char string[] = "1 2 34 233 45";
    char seps[]   = " ";
    int pos = 0;
    char *token;
    token = strtok( string, seps );
    while( token != NULL )
    {
        m_byte[pos] = atoi(token);
        pos++;
        token = strtok( NULL, seps ); 
    }