Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::string copy from memory

Started by gimp Apr 26, 2004 at 11:32 PM 2 replies 300+ views
Original Post
gimp
gimp
I have a few bytes in ram(memory mapped file). I want to create a std::string from this memory. I know the size of the string, it''s not null terminated. I was hoping that there would be something like string.append(memblock, size) Does such a method exist?
Chris Brodie
nickwinters
nickwinters
If you know the size, just use a pointer and loop through. It''ll be roughly 3 lines.

-Nick
SiCrane
SiCrane
std::string has a constructor that takes a char pointer and the number of characters to use to construct the string. So if buffer is a char pointer to the memory, and you want the first five bytes, then you can do:

std::string test(buffer, 5);
gimp
gimp
I thought so, I''m sure I tested that. I must be feeding it bad data...

Many thanks,,
Chris Brodie

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.