Boost logo

Boost Users :

From: Lynn Allan (l_d_allan_at_[hidden])
Date: 2006-04-08 13:07:32


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.
 


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net