Here is what I'm doing:
- file.write((char *) obj_ref, sizeof *obj_ref);
The output looks like a text file with no formating. Aren't binary files supposed to contain binary numbers? lol. What am I missing?
blanny
Hi. Not sure what your question is.
When you write a binary file it is one char at a time, i.e. one byte at a time.
this could be one character byte, one part of the 4 bytes of a float etc. It does not 'look' like anything. When you open it to see what it 'looks' like, the appearance will depend on what app you are using for this purpose so the 8 bit bytes will be interpreted by the app. individually and will normally make no sense if there is numeric data included.
When writing and reading binary format you must cast the variable to char * so that each byte will be written or read separately. In the case of an array for example, you will need the number of elements in the array plus the size of each array element. e.g. an array of long:
- ifst.read(reinterpret_cast<char *> (myarray), arrsize * 4);
If the array was of type char then:
- ifst.read(myarray, arrsize);