[string] HowTo: equiv of printf("%.5s\n", myBuf10);
I've been puzzled about doing the equivalent of: printf("%.5s\n", myBuf10); and also: printf("%.*s\n", 5, myBuf10); std::string myBuf10("0123456789"); const char* pBuf = myBuf10.c_str(); printf("%.5s\n", pBuf); printf("%.*s\n", 5, pBuf); cout << myBuf10.substr(0, 5) << endl; // works ok -> 01234 cout << setw(5) << myBuf10 << endl; // 0123456789 I've used: myBuf10.substr(0, 5); and also ostringstream but it seems like there might be a simpler way. setw(5) doesn't work to accomplish truncation (at least for me). It seems more for padding. Am I unaware of some iomanip functionality to accomplish this? I suppose I could do something like: char hack = myBuf[5]; myBuf[5] = '\0'; .... do something myBuf[5] = hack; (may or may not be portable?) Note: newbie with std::string ... using Microsoft vc7.1 compiler Thanks.
Lynn Allan wrote:
I've been puzzled about doing the equivalent of: printf("%.5s\n", myBuf10); and also: printf("%.*s\n", 5, myBuf10); Note: newbie with std::string ... using Microsoft vc7.1 compiler
Thanks.
string isn't a Boost library. You're unlikely to get any help here. Try posting to comp.lang.c++ on Google Groups.
Paul Giaccone wrote:
Lynn Allan wrote:
I've been puzzled about doing the equivalent of: printf("%.5s\n", myBuf10); and also: printf("%.*s\n", 5, myBuf10); Note: newbie with std::string ... using Microsoft vc7.1 compiler
Thanks.
string isn't a Boost library. You're unlikely to get any help here. Try posting to comp.lang.c++ on Google Groups.
Ture, but format is a Boost library. You might have a look at: http://www.boost.org/libs/format/doc/format.html You can use format with a std::stringstream and then extract an std::string. Jeff
Jeff Garland wrote:
Paul Giaccone wrote:
Lynn Allan wrote:
I've been puzzled about doing the equivalent of: printf("%.5s\n", myBuf10); and also: printf("%.*s\n", 5, myBuf10); Note: newbie with std::string ... using Microsoft vc7.1 compiler
Thanks.
string isn't a Boost library. You're unlikely to get any help here. Try posting to comp.lang.c++ on Google Groups.
Ture, but format is a Boost library. You might have a look at:
http://www.boost.org/libs/format/doc/format.html
You can use format with a std::stringstream and then extract an std::string.
Ah, my mistake. I didn't spot that. Apologies. Paul
This seems to work ok using boost::format:
#include <iostream>
#include
Lynn Allan wrote:
I've been puzzled about doing the equivalent of: printf("%.5s\n", myBuf10); and also: printf("%.*s\n", 5, myBuf10); Note: newbie with std::string ... using Microsoft vc7.1 compiler
Thanks.
string isn't a Boost library. You're unlikely to get any help here. Try posting to comp.lang.c++ on Google Groups.
This seems to work ok using boost::format:
#include <iostream>
#include
I've been puzzled about doing the equivalent of: printf("%.5s\n", myBuf10); and also: printf("%.*s\n", 5, myBuf10);
std::string myBuf10("0123456789"); const char* pBuf = myBuf10.c_str(); printf("%.5s\n", pBuf); printf("%.*s\n", 5, pBuf); cout << myBuf10.substr(0, 5) << endl; // works ok -> 01234 cout << setw(5) << myBuf10 << endl; // 0123456789
I've used: myBuf10.substr(0, 5); and also ostringstream but it seems like there might be a simpler way. setw(5) doesn't work to accomplish truncation (at least for me). It seems more for padding. Am I unaware of some iomanip functionality to accomplish this?
I suppose I could do something like: char hack = myBuf[5]; myBuf[5] = '\0'; .... do something myBuf[5] = hack; (may or may not be portable?)
Note: newbie with std::string ... using Microsoft vc7.1 compiler
Thanks.
participants (3)
-
Jeff Garland
-
Lynn Allan
-
Paul Giaccone