1

This is not the actual code, but this represents my problem.

std::string str1 = "head";
char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::vector<char> mainStr(buffer, buffer + strlen(buffer));

I want to put str1 and str2 to mainStr in an order:

headbody\0bodyfoot

So the binary data is maintained. Is this possible to do this?

PS: Thanks for telling the strlen part is wrong. I just used it to represent buffer's length. :)

3
  • Where do you want to put it? A string, a vector, a stream? Commented Dec 30, 2010 at 12:06
  • @Antonio Perez I want to put it in the mainStr variable as I've said :). It's std::vector<char> Commented Dec 30, 2010 at 12:08
  • As a side note: Why do you want to use a std::vector? A std::string can also store null characters. Commented Dec 30, 2010 at 12:29

7 Answers 7

3

There should be some way of defining length of data in "buffer". Usually character 0 is used for this and most of standard text functions assume this. So if you use character 0 for other purposes, you have to provide another way to find out length of data.

Just for example:

char buffer[]="body\0body";
std::vector<char> mainStr(buffer,buffer+sizeof(buffer)/sizeof(buffer[0]));

Here we use array because it provides more information that a pointer - size of stored data.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing out the difference between char buffer[] and char buffer*, as sizeof() operates on them differently!
2

You cannot use strlen as it uses '\0' to determine the end of string. However, the following will do what you are looking for:

std::string head = "header";
std::string foot = "footer";
const char body[] = "body\0body";

std::vector<char> v;
v.assign(head.begin(), head.end());
std::copy(body, body + sizeof(body)/sizeof(body[0]) - 1, std::back_inserter<std::vector<char> >(v));
std::copy(foot.begin(), foot.end(), std::back_inserter<std::vector<char> >(v));

Because the character buffer adds an NUL character at the end of the string, you'll want to ignore it (hence the -1 from the last iterator).

Comments

2

btw. strlen will not work if there are nul bytes in your string!

The code to insert into the vector is:

front:

mainStr.insert(mainStr.begin(), str1.begin(), str1.end());

back:

mainStr.insert(mainStr.end(), str2.begin(), str2.end());

With your code above (using strlen will print)

headbodyfoot

EDIT: just changed the copy to insert as copy requires the space to be available I think.

Comments

1

You could use std::vector<char>::insert to append the data you need into mainStr.

Something like this:

std::string str1 = "head";
char buffer[] = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";

std::vector<char> mainStr(str1.begin(), str1.end());
mainStr.insert(mainStr.end(), buffer, buffer + sizeof(buffer)/sizeof(buffer[0]));
mainStr.insert(mainStr.end(), str2.begin(), str2.end());

Disclaimer: I didn't compile it.

1 Comment

strlen will stop at the first null byte in buffer
1

You can use IO streams.

std::string str1 = "head";
const char *buffer = "body\0body"; // Original code has nullbytes;
std::string str2 = "foot";
std::stringstream ss;
ss.write(str1.c_str(), str1.length())
  .write(buffer, 9) // insert real length here
  .write(str2.c_str(), str2.length());
std::string result = ss.str();

std::vector<char> vec(result.c_str(), result.c_str() + result.length());

Comments

0

str1 and str2 are string objects that write the text.

I wish compilers would fail on statements like the declaration of buffer and I don't care how much legacy code it breaks. If you're still building it you can still fix it and put in a const.

You would need to change your declaration of vector because strlen will stop at the first null character. If you did

char buffer[] = "body\0body";

then sizeof(buffer) would actually give you close to what you want although you'll get the end null-terminator too.

Once your vector mainStr is then set up correctly you could do:

std::string strConcat;
strConcat.reserve( str1.size() + str2.size() + mainStr.size() );
strConcat.assign(str1);
strConcat.append(mainStr.begin(), mainStr.end());
strConcat.append(str2);

if vector was set up using buffer, buffer+sizeof(buffer)-1

Comments

0
 mainStr.resize(str1.length() + str2.length() + strlen(buffer));  
 memcpy(&mainStr[0], &str1[0], str1.length());  
 memcpy(&mainStr[str1.length()], buffer, strlen(buffer));  
 memcpy(&mainStr[str1.length()+strlen(buffer)], &str2[0], str2.length());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.