I am writing to a binary using a structure containing only char [32]. I basically need to format each data block by doing various calculations on string arrays and concatenating the results. I am trying to copy std :: string array to char array without null termination. The more I read about it, the more I get embarrassed. If I do this:
struct block{
char data[32];
};
block blocks[2048];
std::string buffer;
buffer = "12345678123456781234567812345678";
strcpy(blocks[0].data, buffer.c_str());
I get an error because adding a null terminator using c_str () makes the string length 33. If I subtract one of the char from the string, it works, but then I have a null terminator to want. I can do the following:
strcpy(blocks[0].data, "12345678123456781234567812345678");
but I want to build a string first, as it often involves combining multiple strings from different arrays. For example, I can do this with std :: string:
std::string buffer = stringArray1[0] + stringArray2[0];
strcpy(blocks[0].data, buffer.c_str());
but then again I have a zero limiter. I would just like to copy exactly the characters to std :: string without a null terminator.
I am using VC ++ 6.0.
source
share