MFC CStdioFile

读Text文件

 

void CNWiReworkDlg::ReadHexFile()
{
    using namespace std;
    CStdioFile file;
    file.Open(hexFilePath, CFile::modeRead | CFile::typeText);
    CString line;
    vData.clear();
    while (file.ReadString(line))
    {
        int lineLength = line.GetLength();
        if (lineLength > 11)
        {
            AnalyHexFile(line, lineLength);
        }
    }
    file.Close();
}

void CNWiReworkDlg::AnalyHexFile(CString line,int lineLength)
{
    using namespace std;

    
    if (line.Mid(7, 2) == "00")
    {
        line = line.Mid(9, lineLength - 11);
        CString oneByte;
        for (int i = 0; i < line.GetLength(); i += 2)
        {
            oneByte = line.Mid(i, 2);
            const size_t strsize = (oneByte.GetLength() + 1) * 2;
            char * pOneByte = new char[strsize];
            size_t sz = 0;
            wcstombs_s(&sz, pOneByte, strsize, oneByte, _TRUNCATE);
            vData.push_back(strtol(pOneByte, NULL, 16));
            
        }
    
    }
}

读Binary

void CNWiReworkDlg::ReadBinaryFile()
{
    using namespace std;
    CStdioFile file;
    file.Open(hexFilePath, CFile::modeRead|CFile::typeBinary);
    int len = file.GetLength();
    char* buf = new char[len + 1];

    file.Read(buf, len);
    file.Close();
    vData.clear();
    vHeadData.clear();

    for (int i = 0; i < 256; i++)
    {
        vHeadData.push_back(buf[i]);
    }

    for (int i = 256; i < len; i++)
    {
        vData.push_back(buf[i]);
    }

    delete buf;

}

 

void CNWiReworkDlg::WriteBinaryFileTest()
{
    using namespace std;
    CStdioFile file;
    CString filePath = _T("d:\\test.bin");
    file.Open(filePath, CFile::modeCreate | CFile::modeReadWrite |CFile::typeBinary);
    char* tempChar = new char[_dataSize];
    for (int i = 0; i < _dataSize; i++)
    {
        //vector<byte> _binDatas;
        tempChar[i] = _binDatas[i];    
    }
    file.Write(tempChar, _dataSize);
    file.Close();
    delete tempChar;

}

 

posted @ 2017-10-27 16:58  ike_li  阅读(399)  评论(0编辑  收藏  举报