manhon
manhon

Reputation: 683

c++ fprintf prints only first string

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

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490713

You don't normally want to use fprintf and company in C++.

fileout << 10 << ": " << hi << ", " << bye;

Upvotes: 0

Paul Draper
Paul Draper

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());

See fprintf and c_str().

Though C++ code will typically use the C++ I/O functions.

Upvotes: 5

Related Questions