VC MFC 字符串写入txt

void CMy9_1Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
char name[4][5]={"nums","leng","fade","step"};
double num[4]={1.342,2.235,3.644,4.678};
int i=0;
CFile fout;
CString gs,f;
fout.Open(_T("D:\\TEXT.txt"),CFile::modeCreate|CFile::modeWrite);
gs=CString("set%s=%g\r\n");
for(i=0;i<4;i++)
{
f.Format(gs,name[i],num[i]);
fout.Write(f,f.GetLength());
}
fout.Close();
}
这是我的代码,输出结果又乱码还没有换行
s e t nums leng fade step 烫烫烫烫旞s e t leng fade step 烫烫烫烫旞 s e t fade step 烫烫烫烫旞 烫蘳 e t step 烫烫烫烫旞 烫烫#

求指导,谢谢!!!

在工程配置中选择:多字节字符集!


追问

我只是修改了字符集--使用多字节字符集,提示这样一个错误
1>mt.exe : general error c101008a: Failed to save the updated manifest to the file ".\Debug\9_1.exe.embed.manifest". Bpo。

追答

把工程清理一下再重新编译。
Build -> Rebuild

或者
Build -> Clean

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-02
你的工程是UNICODE型的,所以要用WCHAR数组。我帮你改了,你试试:
WCHARname[4][5]={L"nums",L"leng",L"fade",L"step"};
double num[4]={1.342,2.235,3.644,4.678};
int i=0;
CFile fout;
CString gs,f;
fout.Open(_T("D:\\TEXT.txt"),CFile::modeCreate|CFile::modeWrite);
gs=CString(_T("set%s=%f\r\n"));
for(i=0;i<4;i++)
{
f.Format(gs,name[i],num[i]);
fout.Write(f,f.GetLength());
}
fout.Close();追问

你好,按照你给出的程序运行了,结果好多了。

但是还有两个问题:1、四个句子没有换行;2、double类型的数据失真了。还有一个附加问题就是如何创建txt使其编码为ansi。谢谢

本回答被提问者采纳
第2个回答  2013-09-02
CStdioFile m_fileW;
m_fileW.Open( "D:\\TEXT.txt", CFile::modeNoTruncate|CFile::modeReadWrite)
m_fileW.SeekToEnd();
m_fileW.WriteString("sssssssr\n");
相似回答