How do I read a binary file in C++ if I generate it in Java?
Your problem is that you are using ObjectOutputStream
to write the data. This encodes the object graph in a Java-specific form intended to be read with ObjectInputStream
. To make the data stream compatible with C++ you would need to do one of two things:
- Implement in C++ code that understands the output format produced by
ObjectOutputStream
-- i.e. re-implement in C++ what Java does inObjectInputStream
. This is NOT recommended. - Write your data out from Java using a standard
FileOutputStream
, in a serialized format that you define, that then can be read by your C++ code. How you specify and implement this is up to you but can be very simple, depending on the complexity of your data.