Below are 2 code snippets (which are a concise form of my code) which I'm using for writing and reading a binary file.