假设ListBox中有以下内容:line1
line2
line3
line4
我现在想将ListBox里的这4行文字写入文本文件中,我是这么做的
void CNewChat::OnOutput()
{
// TODO: Add your control notification handler code here CString str,fileName("");
CFile file;
/*********************这部分不管,这是我用来组建的文件名***************************
fileName.Insert(0,myNick);
fileName.Insert(fileName.GetLength()+1,"-");
fileName.Insert(fileName.GetLength()+1,goalName);
fileName.Insert(fileName.GetLength()+1,".txt");
/*********************这部分不管,这是我用来组建的文件名*************************** file.Open(fileName,CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);
for (int i=0;i<m_newChatList.GetCount();i++)
{
m_newChatList.GetText( i, str);
file.Write(&str,str.GetLength());
}
file.Close();
}可是问题来了,程序没有错误,文件也能创建,但是我发现里面的内容是乱码,影响到了我后面对文件的读取
我的读取是这么写的
void COpenfileDlg::OnButton1()
{
m_newChatList.ResetContent(); //创建文件对话框
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||")); if (dlg.DoModal() == IDOK)
{
CString strPathName = dlg.GetPathName(); CStdioFile file;
if (!file.Open(strPathName, CFile::modeRead))
{
::AfxMessageBox(_T("文件打开失败。"));
return;
} //读文件
CString strText = _T("");
while (file.ReadString(strText))
{
m_newChatList.AddString(strText);
}
//关闭文件
file.Close();
}
}这样读出来的也是乱码纠结了。。
line2
line3
line4
我现在想将ListBox里的这4行文字写入文本文件中,我是这么做的
void CNewChat::OnOutput()
{
// TODO: Add your control notification handler code here CString str,fileName("");
CFile file;
/*********************这部分不管,这是我用来组建的文件名***************************
fileName.Insert(0,myNick);
fileName.Insert(fileName.GetLength()+1,"-");
fileName.Insert(fileName.GetLength()+1,goalName);
fileName.Insert(fileName.GetLength()+1,".txt");
/*********************这部分不管,这是我用来组建的文件名*************************** file.Open(fileName,CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);
for (int i=0;i<m_newChatList.GetCount();i++)
{
m_newChatList.GetText( i, str);
file.Write(&str,str.GetLength());
}
file.Close();
}可是问题来了,程序没有错误,文件也能创建,但是我发现里面的内容是乱码,影响到了我后面对文件的读取
我的读取是这么写的
void COpenfileDlg::OnButton1()
{
m_newChatList.ResetContent(); //创建文件对话框
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||")); if (dlg.DoModal() == IDOK)
{
CString strPathName = dlg.GetPathName(); CStdioFile file;
if (!file.Open(strPathName, CFile::modeRead))
{
::AfxMessageBox(_T("文件打开失败。"));
return;
} //读文件
CString strText = _T("");
while (file.ReadString(strText))
{
m_newChatList.AddString(strText);
}
//关闭文件
file.Close();
}
}这样读出来的也是乱码纠结了。。
解决方案 »
- 使用结构体有必要经常使用memset清空内存吗?
- 请问File: i386\chkesp.c Line: 42是什么错误?我在一台vc6下运行没这个错误,而到另一台vc下出现了这个错误?
- 用VC编写的修改电脑分辨率的程序,可以修改纯平显示器的分辨率但是不能修改液晶显示器是怎么回事?
- 没分了,但还是想斗胆问一下
- 求一UDP传文件的源代码
- 安装visual c++时最后提示javasign.dll 无法写注册到数据库
- 请问格式化硬盘德API函数是那个,急呀
- mfc单文档视图分割后视图间的通信问题
- 关于CCD和VC
- 请问有没有VC + BCB 好手,怎样把.dcu转换成VC能用的东东?
- [急切求助]如何修改IDC_STATIC背景颜色(在线等)
- 用VS2008打开用VS2003建立的工程时遇到的问题
str.releasebuffer();
file.Write(str.getbuffer(),str.GetLength());
使用unicode编程就可以了。
写进去的是二进制格式,当然打开是乱码建议改为文本格式化读写
我這邊繁體系統,寫工程遇到好多亂碼問題。只能用unicode編程解決。
extern CStdioFile f;
char buf[] = "test string";f.WriteString( buf );