"Pep" <pe**********@yahoo.co.ukwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
:
: Victor Bazarov wrote:
: Pep wrote:
: I have to interface to an older library that uses strings and there
is
: no alternative. I need to pass a string that is padded with null
: bytes. So how can I append these null bytes to the std::string?
: >
: Probably using "append" member... Have you RTFM?
: >
:
: Yes but it was suggested to me that there was a way that did not
: involve using append, so as I could not find any way I decided to ask
: if anyone knew of one of those "tricks".
:
: Guess it will have to be append then.
And what's wrong with it ?
s.append( 5, '\0' ); // appends five NULs, pretty straightforward?
: Yes I know it would be better to use something like a vector but I
do
: not have that option.
: >
: In your case it probably does not matter. Why do you think that
'vector'
: has any advantage?
:
: I read something in a previous posting that suggested vectors would be
: better though as you, I do not understand how other than perhaps the
: poster meant that you should not in theory have nulls in strings.
Vector historically had to be used if you wanted write
access *and* contiguous storage (because only .data()
and .c_str() were guaranteed to return a pointer to
contiguous storage, and the returned buffer is *const*).
But using &s[0] has always worked in practice,
and the standard has been revised to allow this
[ contiguity is now always guaranteed ]
: Yes I know that I will not be able to use std::string.c_str() but
will
: instead have to use std:;string.getData().
..c_str() might have an advantage over .data() in your case:
it will append an additional NULL char to the returned buffer ;)
Cheers --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form