printf %s打印字符串 出现乱汉字问题解决
printf 基础定义
头文件:#include <stdio.h>
printf()函数是最常用的格式化输出函数,其原型为:
int printf( char * format, … );
printf()会根据参数 format 字符串来转换并格式化数据,然后将结果输出到标准输出设备(显示器),直到出现字符串结束(’\0’)为止。
参数 format 字符串可包含下列三种字符类型:
一般文本,将会直接输出
ASCII 控制字符,如\t、\n 等有特定含义
格式转换字符
乱码原因
因为%s表示a char*,而不是a std::string.
要想获得正确的输出:
1)使用s.c_str(),用c_str来获取与字符串内容等效的c-string
2)使用 iostreams:
实例
string s("abc");
printf("