I tried with below code:
String hi = "hi";
String bye = "bye";
fprintf(fileout, "%d: %s, %s", 10, hi, bye); //fail
fprintf(fileout, "%d: %s, %s", 10, "hi", "bye");//ok
however, this cannot write hi bye to the text file. What is wrong?
fprintf and related functions are C functions.
You need a "C string", which is a null-terminated character array (char * or char const *), not a C++ string (std::string).
fprintf(fileout, "%d: %s, %s", 10, hi.c_str(), bye.c_str());
Though C++ code will typically use the C++ I/O functions.
String that manhon is using is likely not std::string. Note that the capitalization differs. manhon, check whether your String class has a member function similar to std::string::c_str() and try using that.char * (or const char * or char const *).You don't normally want to use fprintf and company in C++.
fileout << 10 << ": " << hi << ", " << bye;
std::setw and std::setprecision.