用下面的格式写数据 
file.Open("save.txt",CFile::modeCreate|CFile::modeWrite);
   file.Write(&data,sizeof(float));//flaot型
   file.Write(&data1,sizeof(float));//float型
   file.Write(&strdata,strdata.GetLength());//CString型
            file.Write(&strdata1,strdata1.GetLength());//CString型
请问怎样把数据读出来?我只会读float型的,CString型不知怎样读。
if(file1.Open("save.txt",CFile::modeRead))
{
        file1.Read(&buf,sizeof(float));//读float型
        file1.Read(&buf1,sizeof(float));//读float型
        file1.Read(bstr.LockBuffer(), file1.GetLength());//不行
        file1.Read(bstr1.LockBuffer(), file1.GetLength());//读不出        
 }

解决方案 »

  1.   

    CString 前面加的有   C  那就说明它是一人类,你不能直接 strdata.GetLength() 就来读出数据
    下面我会和你说如何用它
      

  2.   

    file1.Read(bstr.GetBuffer(0), file1.GetLength());
    记得接着要:
    bstr.GetBufferSetLength(file1.GetLength());http://expert.csdn.net/Expert/topic/1603/1603220.xml?temp=.237179
      

  3.   

    首先你要使用  CString 的成员函数,获取其中存放数据的指针,那就  GetBufferLPTSTR GetBuffer( int nMinBufLength );
    throw( CMemoryException );Return ValueAn LPTSTR pointer to the object’s (null-terminated) character buffer.ParametersnMinBufLengthThe minimum size of the character buffer in characters. This value does not include space for a null terminator.ResReturns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.The buffer memory will be freed automatically when the CString object is destroyed. int nstrlength = strData.GetLength()LPCTSTR  szData;
    szData = strData.GetBuffer( nstrlength );取出存放的字符数据的指针
     filw.write( szData,nstrlength );
    strData.ReleaseBuffer();
    OK!  这次你的 CString 中的字符数据就存进文件中去了
      

  4.   

    读出的时候反过来吧,先存到一个内存中,再传给 CString
      

  5.   

    DWORD dFileLength=file.GetLength();
    char* ch=new char[dFileLength+1];
    file.ReadHuge(ch,dFileLength);
      

  6.   

    if(file1.Open("save.txt",CFile::modeRead))
    {
     DWORD dFileLength=file.GetLength();
    char* ch=new char[dFileLength+1];
    file.ReadHuge(ch,dFileLength);
    MessageBox(ch);       
    }