I'm trying to read a binary file header which consists of a string and an integer. Whilst text-reading is not a problem, binary file streams prove to be much more challenging, getting me stuck at this place - my current code reads the first value, the string, perfectly. The second value is a hexadecimal, and when I try to read it, my code displays a '.' and a long empty string.
Here is my code
//First open the file std::ifstream file(filename, std::ios::in | std::ios::binary); //Then check if it opened if(!file) { //Error opening the file - no point running this. Check your file! fprintf(stderr, "An error occured whilst opening the file '%s'.\n" "Please ensure that the file exists, and that you have\n" "read access to it.\n", filename); exit(-1); } char magic[4]; //Read and store the magic number file.read(magic, 4); printf("Header: %s\n", magic); // This works char version[4]; file.read(version,4); printf("Version: %i\n", atoi(version)); //This returns 'Version: 0' (e.g. atoi fails) printf("Version: %s\n", version); //This returns 'Version: .'
Whilst the first read works, the second doesn't. I've been staring at this code for a while now, and I looked at examples on the internet, but I can't figure out how my code is different from the code that does work. Perhaps it's just me being tired.
Thanks in advance.