c ++ fprintf只打印第一个字符串

man*_*hon 2 c++ printf

我尝试使用以下代码:

String hi = "hi";
String bye = "bye";
fprintf(fileout, "%d: %s, %s", 10, hi, bye); //fail 
fprintf(fileout, "%d: %s, %s", 10, "hi", "bye");//ok
Run Code Online (Sandbox Code Playgroud)

但是,这不能写到文本文件.怎么了?

Pau*_*per 5

fprintf 和相关的功能是C函数.

您需要一个"C字符串",它是一个以空字符结尾的字符数组(char *char const *),而不是C++字符串(std::string).

fprintf(fileout, "%d: %s, %s", 10, hi.c_str(), bye.c_str());
Run Code Online (Sandbox Code Playgroud)

fprintfc_str().

虽然C++代码通常使用C++ I/O函数.