printf的问题

飞阁 2010-09-20 09:44:00

int lo,hi;
CString str;
CStdioFile fFibo;
fFibo.Open((LPCTSTR)"FIBO.dat",CFile::modeWrite|CFile::modeCreate);
str.Format(_T("%s\n"),"Fibonacci sequencee, less than 100 :");
printf("%s\n",(LPCTSTR)str);
fFibo.WriteString(str);

lo=hi=1;

str.Format(_T("%d\n"),lo);
printf("%s\n",(LPCTSTR)str);
//cout<<str;

while(hi<100)
{
str.Format(_T("%d\n"),hi);
printf("%s\n",(LPCTSTR)str);
//cout<<str;
fFibo.WriteString(str);
hi=hi+lo;
lo=hi-lo;
}

fFibo.Close();
getchar();


printf输出为什么只有一位,%s不是该输出字符串的么?比如,89,只输出8
...全文
117 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
MichaelBomb 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 d_dream 的回复:]
可是用cout输出的好像是地址,CString该如何输出?
[/Quote]

cout<<(LPCTSTR)str<<endl;

这样试看看。
某某9 2010-09-20
  • 打赏
  • 举报
回复
string是个类,不是字符串,要printf("%s\n",(LPCTSTR)str.c_str());转化为c风格的字符串才可以正常输出。。

string的标准输出方法是用cout<<
飞阁 2010-09-20
  • 打赏
  • 举报
回复
可是用cout输出的好像是地址,CString该如何输出?
MichaelBomb 2010-09-20
  • 打赏
  • 举报
回复
建议使用 cout<<string<<endl;

二楼正解。
pengzhixi 2010-09-20
  • 打赏
  • 举报
回复
用Getbuffer来转型成char*
乐CC 2010-09-20
  • 打赏
  • 举报
回复
CSting和Char*是不能直接这样转换的
Csuxiaowu 2010-09-20
  • 打赏
  • 举报
回复
楼上所言甚是
  • 打赏
  • 举报
回复
printf的字符串是c类型的,也就是char *....
cout << string 是因为string重载了<<,但string不支持printf的重载
chengzhe 2010-09-20
  • 打赏
  • 举报
回复
Unicode? 使用wsprintf看看??
majinguang2004 2010-09-20
  • 打赏
  • 举报
回复
string是个类,不是字符串,要printf("%s\n",(LPCTSTR)str.c_str());转化为c风格的字符串才可以正常输出,或者用cout<<string<<也可以正常输出,2个方法建议选择第二种
ShayneZHU 2010-09-20
  • 打赏
  • 举报
回复
二楼正解

64,653

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧