Help in reading Binary files

I was trying this code and couldn't run it.
Includes
1
2
3
#include <iostream>
#include <fstream>
using namespace std;


1
2
3
4
        ifstream file;
        file.open("KeyMap.dat",ios::in|ios::binary);
        file.read(reinterpret_cast<char *>(KeyMap), sizeof(KeyMap) * 18); //*
        file.close();


Nothing happens..
In the point where I marked *, the execution shifts to somewhere else..In the Debugger's log, it shows
"In __cxa_throw () ()", In the watch window it shows file = <incomplete type>.
And nothing is read from the file..
Please help.
It's probably because you have a problem defining or initializing KeyMap. Seeing all of your code would help, because mind-reading costs extra! ;)
KeyMap is declared as
1
2
3
4
5
struct __KeyMap
        {
            enumMoves Moves;
            SDLKey Key;
        }KeyMap[2][9];
OK, that's helpful. How do you declare and initialize it?
Well couldn't get your point. there's no initialization. That's all there is to declaration and initialization. And the struct is a public one.
I was asking where in your code is the structure defined. Anyway, in your file.read() does the file contain 18 keymaps? On the line before that, did the file open OK?
That structure is defined inside a class. I know that its not the problem with the structure. And yes the file do contains 18 keymaps. file.good() returned 1. Thanks in advance
Well I can't help you if you will not post your code. Good luck.
Can post my code,, except that it is more than 2000 lines.. :((,, I think i will now resort to my old method using stdio.h's fread method. Thanks kooth..
sizeof(KeyMap) == sizeof(__KeyMap) * 18

Try sizeof(KeyMap). It looks like you're potentially overflowing 'KeyMap'
Last edited on
Topic archived. No new replies allowed.