我需要将一个静态文本的内容保存在txt文件中,并且也可以将txt的内容读取出来显示在静态文本中
保存的代码是
CString strCaption;
m_STAFreq.GetWindowText(strCaption); //m_STAFreq是个静态文本控件
CFileDialog fileDlg(FALSE);              //构造一个文件另存为对话框
fileDlg.m_ofn.lpstrTitle=_T("保存跳频方案");
fileDlg.m_ofn.lpstrFilter=_T("Text Files(*.txt)\0*.txt\0ALL Files(*.*)\0*.*\0\0");
fileDlg.m_ofn.lpstrDefExt=_T("txt");   //定义缺省文件扩展名
if(IDOK==fileDlg.DoModal())              //打开文件另存为对话框
{
CFile file(fileDlg.GetPathName(),CFile::modeCreate | CFile::modeWrite);           //以写的方式创建新文件
file.Write(strCaption,strCaption.GetLength()*sizeof(wchar_t));    //往文件写入数据
file.Close;
}
读取的代码是
    CFileDialog fileDlg(TRUE,_T(""),_T(""),OFN_FILEMUSTEXIST |OFN_HIDEREADONLY,_T("文件类型(*.txt)|*.txt|所有文件(*.*)|*.*|"));
CString filePathName;
CStdioFile file;
CString result;
if(fileDlg.DoModal() == IDOK)
{
filePathName = fileDlg.GetPathName();
}
if(filePathName == "")  
return;
if(!file.Open(filePathName,CFile::modeRead))
{
MessageBox(_T("文件无法打开!"));
return;
} CString strLine,strTemp; while(file.ReadString(strLine))
{
strLine.GetBufferSetLength(strLine.GetLength());
result +=strLine;
}
m_STAFreq.SetWindowText(result);
delete fileDlg;//释放分配的对话框
为什么保存出来的txt是正确的,读出来总是只能读第一个字符呢?并且有可能最后还显示乱码呢?

解决方案 »

  1.   

    while(file.ReadString(strLine))     {         strLine.GetBufferSetLength(strLine.GetLength());         result +=strLine;     } 单步调试一下,看看里面的变量的值
      

  2.   

    是不是UNICODE,你换成多字节看看
      

  3.   

    我怀疑是unicode遇见了中文时候的情况.
      

  4.   

     delete fileDlg;//释放分配的对话框  ???????fileDlg又不是new的 干嘛要delete??
      

  5.   

    不使用unicode字符,一般不会出这个问题,你的问题就是unicode造成的,