A variable 'buffer' is an std::string that has a lot of characters in it, not just the usual "ABC123" that one would find in normal printing (any character, actually, including '\n' and '\r'). I'm having serious issues getting the stored characters within buffer, however, using anything involving std::string::substr(). I can do it without issue using buffer.at(location), but this seems costly in terms of speed (and I am doing operations involving hundreds of kilobytes worth of characters).
Here's an example of the problem:
printf("By index: %c%c%c%c'",buffer.at(3),buffer.at(4),buffer.at(5),buffer.at(6));
OUTPUTS: '½⌂ '
printf("by substring: '%s'\n",buffer.substr(3,4).c_str());
OUTPUTS: '½⌂'//note the missing whitespace, null, or whatever the case may be
This truncated version is useless to me because I need to know the precise value of all four characters (in this case it's just four, in other's it's thousands). I'm also weary of just assuming those values have a certain meaning - is std::string::c_str() truncating at the first null, thus leaving all following values indeterminate? Or does it simply shave off all the last null characters, so all following characters can safely be assumed null? And, perhaps more importantly, is null the only one that causes this truncation? If not, then I can't tell anything by its lack than that it may be one of those.
The only way around this I see is
int pos,len;//starting position and length, as in std::string::substr()
char* t_data;//temporary data storer
t_data = new char[len];
for(int i=0;i<len;i++)
t_data = buffer.at(pos+i);
//use t_data, in this case it's actually an integer so atoi(t_data)
delete[] t_data;
but this appears slower and with much more overhead. Is there no other way? Or am I somehow misunderstanding std::string::substr()?