I have a binary file Tetris.ch8 which is 494 bytes. I am trying to open that file from my emscripten project. I used –preload-file to load the file. When I am trying to open the file and read the contents, only 21 bytes are present.
void Emulator::LoadROM(char* filename) {
std::ifstream fs(filename, std::ios::binary);
std::stringstream buf;
buf << fs.rdbuf();
std::cout << buf.str() << buf.str().length() << std::endl;
return;
}
This is one of the methods I tried. It prints random characters and 21 as length. I also tried std::filesystem::file_size, ftell, etc to try to get the size. All of them are printing 21.
But when I try it separately, without emscripten, with mingw g++, The file size is printed as 494 (expected)
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
char filename[] = "resources/Tetris.ch8";
std::ifstream fs(filename, std::ios::binary);
std::stringstream buf;
buf << fs.rdbuf();
std::cout << buf.str() << buf.str().length() << std::endl;
return 0;
}
This is working correctly.
Why is the same code behaving differently in emscripten?
I tried it with non binary file, I changed the contents of Tetris.ch8 to “Hello”. This is working fine in emscripten. Length is 7 and string is read correctly too.
I don’t know what I’m missing here.
Master Yoda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2