在vs2008,做MFC,在OnButton1中加入以下代码,功能是将编辑框的文本写入txt,但结果不正确,打开1.txt有时候
字符中有空格,有时候编辑框的内容不能全部写入1.txt          CFile file(L"1.txt",CFile::modeCreate|CFile::modeWrite);
LPTSTR  str1=new WCHAR[100];
m_edit.GetWindowText(str1,100);
str+=str1;
 
file.Write(str.GetBuffer(),str.GetLength());
 file.Close();

解决方案 »

  1.   

    用 Flush() 函数刷新一下,再关闭文件试试
      

  2.   

    不行啊,1.txt写入的每个字符间都有空格,并编辑框的内容不能全部写入
      

  3.   

    你先把 1.txt 另存为unicode 形式,再往里写
      

  4.   

    CStdioFile fire;
    fire.WriteString(); 用这个类写,不容易出错
      

  5.   

    UNICODE中单字节为0X00造成的。检查一下write的两个输入参数。
      

  6.   

    知道了,file.Write(str.GetBuffer(),str.GetLength()); 的第二个参数得到是字符个数,但str中存储的是unicode是字节数的2倍,改为file.Write(str.GetBuffer(),2*str.GetLength())就对了;不过:1.txt中有刚开始有空格,这是为什么呢? 
      

  7.   

    UNICODE中单字节为0X00造成的。是什么意思?为什么1.txt中刚开始有空格呢? 
      

  8.   

        CFile file(L"1.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeText ); 
      

  9.   

    CFile file(_T("1.txt"),CFile::modeCreate|CFile::modeWrite); 
    CString strText;
    m_edit.GetWindowText(strText); 
    char prefix[2] = {0xff, 0xfe};
    file.Write(prefix, 2);
    file.Write(strText.GetBuffer(),strText.GetLength()*sizeof(TCHAR)); 
    file.Close();
      

  10.   

    为什么这样写不行呢?编译无法通过
          CFile file(L"1.txt",CFile::modeCreate|CFile::modeWrite); 
          CString strText;
          m_edit.GetLine(0,strText);//得到第0行的内容
          file.Close();error C2664: “int CEdit::GetLine(int,LPTSTR) const”: 不能将参数 2 从“CString”转换为“LPTSTR”
      

  11.   


    谁帮我把这问题回答了我请你吃饭啊为什么这样写不行呢?编译无法通过 
          CFile file(L"1.txt",CFile::modeCreate|CFile::modeWrite); 
          CString strText; 
          m_edit.GetLine(0,strText);//得到第0行的内容 
          file.Close(); error C2664: “int CEdit::GetLine(int,LPTSTR) const”: 不能将参数 2 从“CString”转换为“LPTSTR”
      

  12.   


    GetLine(int,LPTSTR)这个第二个参数是个指针,CString是一个类,类型不匹配。
    改成这个
    GetLine(0,strText.GetBuffer())
      

  13.   

    这样也不行,GetLine(0,strText.GetBuffer()) 无法得到内容
      

  14.   



    [color=#FF0000]谁帮我把这问题回答了我请你吃饭啊 
    为什么这样写不行呢?编译无法通过 
          CFile file(L"1.txt",CFile::modeCreate|CFile::modeWrite); 
          CString strText; 
          m_edit.GetLine(0,strText);//得到第0行的内容 
          file.Close(); error C2664: “int CEdit::GetLine(int,LPTSTR) const”: 不能将参数 2 从“CString”转换为“LPTSTR
      

  15.   


    CString temp;
    m_edit.GetWindowTextW(temp);
    CFile file(_T("1.txt"),CFile::modeCreate|CFile::modeWrite);
    file.Write(temp,temp.GetLength());
    file.Flush();
    file.Close();这样可以把字母写进去,汉字写不进去,自己再改下
      

  16.   

    m_edit.GetLine(0,strText);
    参数2需要是一个缓冲区,不支持CString的引用可以考虑使用:
    wchart buf[1024];
    memset(buf,0,2048);
    m_edit.GetLine(0,buf);
    strText=buf;
      

  17.   

    将CString strText改成TCHAR *buff= new TCHAR[256];
      

  18.   

    没怎么仔细看楼主的意思。可能是编码问题,如果希望用notepad.exe打开Unicode字符文件需要加标头的。什么标头符号都没有它会默认是ansi编码,你用MultiByteToWideChar函数转一下,打开文件也是这样
      

  19.   

    unicode工程下写文件,也遇到中文写到文件中乱码的问题,在写入之前转换为ASNI就ok了
      

  20.   

    Unicode文本文件开都都有一个 ff fe 还是 fe ff(具体什么忘了)的两字节的标志的,写文件的时候应该先写这个