Reputation: 43
I have problem reading image from file, but when there is raw text it works properly. I read, that any file can be read in binary mode, but my output is just this: \FF\D8\FF\E0 and then it stops. It stops at this character so I am not sure whether it just can't resolve that character or what. Does anyone know what is wrong? Thanks in advance.
Here's my code
char* obsah;
std::string sprava;
std::ifstream is (file.c_str(), std::ifstream::binary);
if(is){
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
obsah = new char [length];
is.read(obsah,length);
sprava = prepinac+"\r\n"+file+"\r\n\r"+obsah;
}
else exit(EXIT_FAILURE);
Upvotes: 2
Views: 262
Reputation: 73366
sprava
is a string. As you use +
to concatenate its components, it manages char* obsah
as a null terminated c-string. So everything after the first null char will not be copied into sprava
.
Edit:
You can have binary data including '\0'
in strings (see here). However you need to be very careful in this approach,because whenever you'd convert your string into char*
pointer that is processed like a null terminated c-string, a part of the string might be ignored. And if you use your string in I/O it might also give weird results.
If you want to proceed however, you could use std::copy()
and a back inserter;
sprava = prepinac+"\r\n"+file+"\r\n\r";
copy(obsah, obsah+length, back_inserter<string>(sprava));
Here an online demo
Upvotes: 2