How can I read an image from a binary file?

I'm trying to put all the assets inside a binary file and then trying to read them from the file itself, without scattering all them around in separate folders to let them exposed. What I'm trying to achieve is something like the way that RPG Maker and Wolf Editor use to join all the images / sounds of the game into a single customized format file (e.g. Data.wolf).

It's a very simple security's method but is enough for what I'm planning.

For now, I have the following code for reading a PNG file inside the binary, that always prints an error's message when it trys to create a new surface from IMG_LoadPNG_RW, saying "Error reading the PNG file."

tString sResult{};

tString sLine{};
while(getline(oRead, sLine)) sResult += sLine + "\n";
}else {
throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
}

SDL_RWops* aRW{};
aRW = SDL_RWFromConstMem(&sResult, sizeof(sResult));

if(!aRW) // Error message

SDL_Surface* aSurface{};

if(!aSurface) // This is where SDL shows "Error reading the PNG file."


EDIT: Omg I made some mistakes, now this is the correctly code. Now it gives a different error: "PNG file corrupted by ASCII conversion"

    char* sBuffer{};
sBuffer = new char[iLength];
}else {
throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
}

SDL_RWops* aRW{};
aRW = SDL_RWFromConstMem(sBuffer, sizeof(sBuffer));

• Try oRead >> std::noskipws; right after opening the file. Jan 14, 2018 at 2:13
• Also, this way of doing things is slightly inefficient. It would be faster to get the file size first, allocate a buffer of needed length, and then do a single read into it. Jan 14, 2018 at 2:14
• @HolyBlackCat I'm sorry, I'm very newbie so how can I do that? (The oRead>>std::noskipws didn't worked) Jan 14, 2018 at 2:16
• Reading binary file line by line can fail in many ways. I assume oRead is a std::ifstream? Don't remove std::noskipws, use .seekg() to move to the end of file, .tellg() to get the size, allocate a buffer of this size, .seekg() back to the beginning, .read() into the buffer. en.cppreference.com/w/cpp/io/basic_istream Jan 14, 2018 at 2:26
• @HolyBlackCat I edited the post with the code that you suggested and keeps giving the same error... "libpng error: Not a PNG file", "Error reading the PNG file." Jan 14, 2018 at 2:42

I was able to find a solution, which was a result of various information I collected here in Gamedev and StackOverFlow.

To increase the range of the solution, I'm going to pass the link from where I got it: https://stackoverflow.com/questions/4317318/how-do-i-pass-an-stdstring-to-a-function-that-expects-char