Let talk about using reinterpret_cast and binary files (C++)

Discussion in 'Computer Programming, Emulation, and Game Modding' started by CuriousTommy, May 28, 2017.

  1. CuriousTommy

    CuriousTommy GBAtemp Fan

    Jul 22, 2014
    United States
    Hello Everyone,

    Recently I have been discovering the magic wonders of the reinterpret_cast function. I have been trying to find a solution for reading and writing any binary file in memory (something like fstream, but for binary files in memory). For example, I want to read from a binary file in a dynamic char array). It seems like reinterpret_cast solves this problem.

    I made a very basic program to see if I was able to have data in a char array be accessed by an int pointer. It felt really satisfying being able to put an ASCII value in the pointer int and have the change be shown in a char array.

    With all that being said, I have also heard that using reinterpret_cast is very dangerous. Is it dumb to use reinterpret_cast for in-memory binary file manipulation, or is it normal to use reinterpret_cast this way? I heard it is used for bit manipulation, but I am not sure if that also applies to reading and writing to binary files in memory.

    I was thinking of doing a char binary reader and writer for the summer as a fun project, but I don't want to develop any bad coding habit that would be considered reckless or extremely dangerous. I will implement this as a class, so there will be error checking to reduce misuse or improper access.