|
Boost Users : |
From: Lynn Allan (l_d_allan_at_[hidden])
Date: 2006-04-10 05:00:56
This seems to work ok using boost::format:
#include <iostream>
#include <boost/format.hpp>
void main(void)
{
std::string myBuf10("1234567890");
std::cout << "Put truncated: "
<< boost::format("%|.5s|\n") % myBuf10
<< " part of " << myBuf10 << std::endl;
}
----- Original Message -----
From: "Lynn Allan" <l_d_allan_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.user
Sent: Saturday, April 08, 2006 11:07 AM
Subject: [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.
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