“printf”字符串打印乱码 [英] "printf" on strings prints gibberish
本文介绍了“printf”字符串打印乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试以下列方式列印字串:
I'm trying to print a string the following way:
int main(){
string s("bla");
printf("%s \n", s);
.......
}
推荐答案
因为%s
表示 char *
,而不是 std :: string
。使用 s.c_str()
或更好的仍然使用iostreams:
Because %s
indicates a char*
, not a std::string
. Use s.c_str()
or better still use, iostreams:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("bla");
std::cout << s << "\n";
}
这篇关于“printf”字符串打印乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文