我尝试使用以下代码:
String hi = "hi";
String bye = "bye";
fprintf(fileout, "%d: %s, %s", 10, hi, bye); //fail
fprintf(fileout, "%d: %s, %s", 10, "hi", "bye");//ok
但是,这无法将 hi bye 写入文本文件。怎么了?
最佳答案
fprintf
及相关函数是C函数。
您需要一个“C 字符串”,它是一个以 null 结尾的字符数组(char *
或 char const *
),而不是 C++ 字符串( std::string
).
fprintf(fileout, "%d: %s, %s", 10, hi.c_str(), bye.c_str());
尽管 C++ 代码通常会使用 C++ I/O functions 。
关于C++ fprintf只打印第一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21669972/