Reputation: 683
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?
Upvotes: 2
Views: 171
Reputation: 490713
You don't normally want to use fprintf
and company in C++.
fileout << 10 << ": " << hi << ", " << bye;
Upvotes: 0
Reputation: 83411
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.
Upvotes: 5