C ++ converts file stream function to use string stream

I have many C ++ classes that read data from a file stream. The functions are as follows.

bool LoadFromFile(class ifstream &file);

I am creating a new function to read from memory instead of a file. So I googled around, and istringstream seems to do the trick without any changes to the code.

bool LoadFromData(class istringstream &file);

Now my question. I need to build this stream to read from a char array. The line does not end with zero, this is pure binary data, and I got an integer with size. I tried to assign it to a string and create a stream from the string, however the string ends after the null character .. and the data is copied.

int size;
char *data;
string s = *data;

How to create a string from a char array pointer without copying data + with indicating the size of the pointer data? Do you know any other solution besides a string?

+5
1

basic_streambuf! .. ( , .)

: string str(data,data+size); ( ).

: istream:

bool LoadFromStream(istream &is);

, , istringstream, ifstream istream ( tcp...):

ifstream file;
istringstream sstream;

LoadFromStream(file);
LoadFromStream(sstream);
+4

All Articles