Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Storing byte stream in std::string

Reply
Thread Tools

Storing byte stream in std::string

 
 
TBass
Guest
Posts: n/a
 
      01-03-2008
Hi,

I'm moving a socket library I wrote from C to C++. In the C version, I
had to malloc char arrays to store incoming communication. My hope was
to use std::string in C++, but then I realized a problem.

While '\0' is a valid string terminator for text, for my purposes it
is a problem. My program regularly gets '\0' as a value (Modbus/RTU
and TCP pass register values as the actual values, not the ASCII
chart). That would be a problem with std::string, I would think, since
it would see the '\0' as the end of a character stream, while, in
actuality, it would be all over the string and not indicate the
termination of the stream.

My question is whether I can write a stream of bytes to std::string,
read the length, and be able to get the whole string back. I expect
that I wouldn't be able to use any of the string functions, but could
I at least get the number of bytes store and get those bytes back at a
later time?

Or should I try a different container?

Thanks in advance,
T
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      01-03-2008
TBass wrote:
> Hi,
>
> I'm moving a socket library I wrote from C to C++. In the C version, I
> had to malloc char arrays to store incoming communication. My hope was
> to use std::string in C++, but then I realized a problem.
>
> While '\0' is a valid string terminator for text, for my purposes it
> is a problem. My program regularly gets '\0' as a value (Modbus/RTU
> and TCP pass register values as the actual values, not the ASCII
> chart). That would be a problem with std::string, I would think, since
> it would see the '\0' as the end of a character stream, while, in
> actuality, it would be all over the string and not indicate the
> termination of the stream.


the representation of std::string doesn't treat '\0' a special
character, which means you an push_back any '\0' at any time.

>
> My question is whether I can write a stream of bytes to std::string,
> read the length, and be able to get the whole string back. I expect
> that I wouldn't be able to use any of the string functions, but could
> I at least get the number of bytes store and get those bytes back at a
> later time?
>
> Or should I try a different container?
>


Well, std::string does NOT guarantee the underlaying memory continuous.

I think std::vector is more like a buffer here, which guarantees
continuous underlying memory.

std::vector<char> buffer;
&buffer[0]; // get the pointer to the first cell.
 
Reply With Quote
 
 
 
 
TBass
Guest
Posts: n/a
 
      01-03-2008

> std::vector<char> buffer;
> &buffer[0]; // get the pointer to the first cell.


Great. I haven't messed with vector yet. I'll give that a try.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-03-2008
On Jan 3, 3:57 am, Barry <(E-Mail Removed)> wrote:

[...]
> Well, std::string does NOT guarantee the underlaying memory
> continuous.


It does in the most recent draft. Since the underlaying memory
is contiguous in all current implementations, and will be
guaranteed contiguous in the next version of the standard, I
think you can pretty much count on it.

> I think std::vector is more like a buffer here, which guarantees
> continuous underlying memory.


Conceptually, of course, I find std::vector< char > a closer
fit. But both should work.

> std::vector<char> buffer;
> &buffer[0]; // get the pointer to the first cell.


You must "resize()" the container (std::string or std::vector)
to the target size before doing this.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
TBass
Guest
Posts: n/a
 
      01-03-2008

> You must "resize()" the container (std::string or std::vector)
> to the target size before doing this.


I did. I went right to vector as the solution, and it is working
nicely. Thank you!
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-04-2008
TBass wrote:
>> You must "resize()" the container (std::string or std::vector)
>> to the target size before doing this.

>
> I did. I went right to vector as the solution, and it is working
> nicely. Thank you!


It should work either in std::string or std::vector, but std::vector is
probably a better fit for binary date since you won't be using std::strings
string manilupating methods anyway.

In one implementation of a socket class I had done I had used std::string
and added each character
buffer += c;
with no problems.

That is one advantage to std::string, the syntax, rather than having to push
back
buffer.push_back(c);
but a small thing.

Either way, you should .reserve() before you start adding to either so the
string or vector won't have to keep resizing itself. Normally it's not much
of an issue but adding one character at a time can cause a large number of
resizes.


--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
how to read/write a characters stream which is either of one byte/2 byte Deep C Programming 6 02-28-2007 01:03 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Problems storing a byte array =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?= ASP .Net 1 01-02-2006 12:46 PM



Advertisments