我尝试使用以下代码:
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)
但是,这不能写到文本文件.怎么了?
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)
虽然C++代码通常使用C++ I/O函数.