在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.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();
fire.WriteString(); 用这个类写,不容易出错
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();
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”
谁帮我把这问题回答了我请你吃饭啊为什么这样写不行呢?编译无法通过
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”
GetLine(int,LPTSTR)这个第二个参数是个指针,CString是一个类,类型不匹配。
改成这个
GetLine(0,strText.GetBuffer())
[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
CString temp;
m_edit.GetWindowTextW(temp);
CFile file(_T("1.txt"),CFile::modeCreate|CFile::modeWrite);
file.Write(temp,temp.GetLength());
file.Flush();
file.Close();这样可以把字母写进去,汉字写不进去,自己再改下
参数2需要是一个缓冲区,不支持CString的引用可以考虑使用:
wchart buf[1024];
memset(buf,0,2048);
m_edit.GetLine(0,buf);
strText=buf;