I need to read n char
s from a binary file into a string
. Currently, what I do is:
static string Read(istream &stream, uint32_t count)
{
auto bytes = unique_ptr<char[]>(new char[count]);
stream.read(bytes.get(), count);
return string(bytes.get(), count);
}
I found the way I deal with the array of char
s quite messy. If I used new
and delete[]
directly, it would make the code messy in another way (I would need to add a local variable for the result). And I'm trying to avoid delete
as a general rule.
Is there a clear way to write this code? The fact that it uses twice as much memory as it needs is probably not a big deal, but fixing that would be nice too.