我读取txt文件,代码如下:
CString str;
CFile f;
f.Open(FileDlg.GetNextPathName(thePosition),CFile::modeReadWrite);
f.Read(str.GetBuffer(f.GetLength()),f.GetLength());
f.Close();
MessageBox(str);原文件内容是这样的
16-20121114
2C183A 03 96.100 4
9D089A 08 120 1
9C052A 03 115 1
9C170A 04 115-125 1
       13 115-125 1
9D189A 13 115.120 1
8D587A 37 80.85 1
8D588A 37 90 1
9D298A 03 110-125 1
2D042A 26 88 1
2D042B 19 100.104 1
2D006A 31 96 1
8D379C 17 80 1
9D280A 02 110.115 1但是读取出来的数据末尾有乱码

解决方案 »

  1.   

    用CStdioFile打开文件,可以用ReadString逐行读取文件内容。
    str.GetBuffer()后,需要ReleaseBuffer释放
      

  2.   

    f.Read(str.GetBufferSetLength(f.GetLength()),f.GetLength()); 
      

  3.   

    GetBuffer后返回的char* 如果是当作const char *(其实不改长度就行)来使用就不需要ReleaseBuffer的
      

  4.   

    楼上正解。在 str+="\0"MessageBox(str);
      

  5.   

    CString strPath,strText;
    CFileDialog file(true,L"file",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"txt|*.txt||",this);
    if (file.DoModal()==IDOK)
    {
          strPath=file.GetPathName();
    }
    CStdioFile  stdfile;
    if (stdfile.Open(strPath,CFile::modeRead))
    {
          CString str;
          while(stdfile.ReadString(str))
          {
              strText+=str;
          }
          MessageBox(strText);
    }