[string] HowTo: equiv of printf("%.5s\n", myBuf10);

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[string] HowTo: equiv of printf("%.5s\n", myBuf10);

l_d_allan
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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] HowTo: equiv of printf("%.5s\n", myBuf10);

Paul Giaccone
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.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] HowTo: equiv of printf("%.5s\n", myBuf10);

l_d_allan
In reply to this post by l_d_allan
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" <[hidden email]>
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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] HowTo: equiv of printf("%.5s\n", myBuf10);

Jeff Garland
In reply to this post by Paul Giaccone
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
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] HowTo: equiv of printf("%.5s\n",myBuf10);

l_d_allan
In reply to this post by Paul Giaccone
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: "Paul Giaccone" <[hidden email]>
Newsgroups: gmane.comp.lib.boost.user
Sent: Monday, April 10, 2006 2:55 AM
Subject: Re: [string] HowTo: equiv of printf("%.5s\n",myBuf10);


> 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.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [string] HowTo: equiv of printf("%.5s\n", myBuf10);

Paul Giaccone
In reply to this post by Jeff Garland
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


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users