Please, help me. I can't read binary file. The file's length is 198944, but my code reads 374. I tried to use fread, ifstream, WinAPI ReadFile. This is the function that reads file:
std::string ReadThisFile(std::string aPath) {
FILE *inputstream = fopen(aPath.c_str(),"rb");
long size;
size_t result;
fseek(inputstream,0,SEEK_END);
size = ftell(inputstream);
rewind(inputstream);
char *buff = new char [size];
result = fread (buff,1,size,inputstream);
std::string ret=buff;
fclose(inputstream);
delete[]buff;
return ret;
}
Any help is needed, thank you!
As long as you're aware of that the std::string
that you return contains binary data, replace
std::string ret=buff;
with
std::string ret(buff, size);
You can't put binary data in a string. Remember that a string is terminated by the special character '\\0'
which is the same as the value zero. If the binary data contains a zero, that is the same as the end of the string.
You should probably use a std::vector<int8_t>
to store binary data.
The problem is here:
std::string ret = buff;
According to this , it:
"Copies the null-terminated character sequence (C-string) pointed by s."
So it stops as soon as a 0x00
character is reached.
If you were to return buff
(which is a dangerous practice) or have your function take a char array as an input parameter and merely store it there, it should work. And you may want to return a length indicator, since otherwise C++ will not know how large the array is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.