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

时间:2014-02-10 05:52:10

标签: 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

然而,这不能写到文本文件。有什么问题?

2 个答案:

答案 0 :(得分:5)

fprintf和相关函数是C函数。

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

fprintf(fileout, "%d: %s, %s", 10, hi.c_str(), bye.c_str());

请参阅fprintfc_str()

虽然C ++代码通常会使用C++ I/O functions

答案 1 :(得分:0)

您通常不希望在C ++中使用fprintf和公司。

fileout << 10 << ": " << hi << ", " << bye;
相关问题