我的代码是
void Cmydlg::OnBnClickedButton3()

 UpdateData();
CFileDialog   dlg(FALSE, _T("TXT"),_T("压降文本"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("文本文件 "),this); 
if(dlg.DoModal()==IDOK) 

CString strText(_T(""));
GetDlgItemText(IDC_EDIT1,strText);;CFile   file; 
file.Open(dlg.GetPathName(),CFile::modeCreate|CFile::modeWrite); 
file.Write((void*)(LPCTSTR)strText,strText.GetLength() ); 
file.Close(); 
} } 
但是我在EDIT1中输入123456保存的只有123,求教这是怎么回事啊

解决方案 »

  1.   

    file.Write((void*)(LPCTSTR)strText, strText.GetLength()*sizeof(TCHAR) );  
      

  2.   

    因为你使用的是unicode编码。
    GetLength返回6,于是写6个字符。但是,“123456”实质是L"123456"。
    即char ch[]={ "1 2 3 4 5 6 " };所以只写了123。class Chars
    {
    public:
    Chars(const CString &str)
    {
    int length=WideCharToMultiByte(CP_ACP,0,str,-1,NULL,0,NULL,FALSE);
    ch=new char[length+1];
    WideCharToMultiByte(CP_ACP,0,str,-1,ch,length,NULL,FALSE);
    }
    char* GetChars()
    { return ch; }
    ~Chars()
    { delete ch; }
    private:
    char* ch;
    };此类将宽字符转成普通字符,这样就不会有问题了。
    CString str;
    Chars chs(str);
    char* cc = chs.GetChars()
    CFile::Write((void*)cc,strlen(cc));