C++: Mixing cout with sprintf and strings

I'm writing a C++ program, writing to sysout using cout. Because iostream's formatting codes are so completely bizarre and incomprehensible to me, I want to use sprintf() to build the output string, and cout to print it. And I'm having the worst time of it, probably because of not completely understanding const.

char buffer[81]; // +sprintf char[] buffer.+ *How is a programmer meant to post code fragments to this forum when brackets, pluses, underscores, and asterisks are interpreted as markup?????*+

string s = "%-" + itos(longest) + "s "; // +itos() converts int to string. it works correctly.+

char * c = s.c_str(); // +convert format string to a char*, which sprintf requires+
* ERROR: invalid conversion from "const char *" to "char *" *

sprintf(buffer, c, (n < wordvec.size()) ? wordvec[n] : ""); //
* WARNING: cannot pass objects of non-POD type ... call will abort at runtime

cout << buffer;

---
.
Can anyone advise me on what is the right way to do this, without having to learn iostream manipulators and other grossness? (s)printf is SO easy....

Thanks,
Chap

2.4GHz MacBook Pro, Mac OS X (10.5)

Posted on Dec 11, 2008 3:01 PM

Reply
4 replies

Dec 11, 2008 5:16 PM in response to ericmeyers


char buffer[81]; // sprintf char[] buffer.
string s = "%-" + itos(longest) + "s "; // itos() converts int to string. it works correctly.
char * c = s.c_str(); // convert format string to a char*, which sprintf requires
ERROR: invalid conversion from "const char *" to "char *" *
sprintf(buffer, c, (n < wordvec.size()) ? wordvec[n] : ""); //
WARNING: cannot pass objects of non-POD type ... call will abort at runtime
cout << buffer;

Dec 11, 2008 5:17 PM in response to Chap Harrison

Chap Harrison wrote:

char buffer[81]; // +sprintf char[] buffer.+ *How is a programmer meant to post code fragments to this forum when brackets, pluses, underscores, and asterisks are interpreted as markup?????*+
string s = "%-" + itos(longest) + "s "; // +itos() converts int to string. it works correctly.+
char * c = s.c_str(); // +convert format string to a char*, which sprintf requires+
* ERROR: invalid conversion from "const char *" to "char *" *
sprintf(buffer, c, (n < wordvec.size()) ? wordvec[n] : ""); //
* WARNING: cannot pass objects of non-POD type ... call will abort at runtime
cout << buffer;
</div>

Wordvec appears to be a std::string. You will have to convert it before you can print it. As for the const problem, there isn't much I can tell you. c_str() returns a const char *. You can't/shouldn't cast that into a char *. In fact, you don't even need to do that at all.

sprintf(buffer, "%-*s", longest, n < wordvec.size()) ? wordvec[n].c_str() : ");

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

C++: Mixing cout with sprintf and strings

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.