C++ mysticism while reading binary

Started by
13 comments, last by Sigutis 15 years, 9 months ago
Now, I have to say I am screwed here. I am writing a map loading feature for my game. It's still in console application stage. The bug appears to be in the waypoint part. The idea is - every monster has 5 positions to walk. Just to look he's busy ;) . First monster_number is loaded, then the loop ( constantly 5 times) loads coordinates for each waypoint into monster_waypoint;
in
is an ifstream object.
p
is just an integer variable used as file position.

for (N=0;N<5;N++){
  for (n=0;n<monster_number;n++){	
    p+=10;
    in.seekg(p);								
    in >> monster_waypoint[N][n].x;
    p+=10;
    in.seekg(p);	
    in >> monster_waypoint[N][n].y;
  }
}
Now, I tried to load my example map - everything looks to be loaded fine. This is fallowed by active object loading, witch is not important in the case.

p+=10;
in.seekg(p);										in >> active_obj_number;
But if I want to check the second monsters fifth waypoints x value (phew) with

cout << monster_waypoint[4][1].x;
Program does not crash but rather ignores code that goes after it. Press any key to continue... The strange part is that if I put
 cout << monster_waypoint[4][0].x; 
BEFORE reading active_obj_number it works perfect. It prints correct value. Also if I ask to print something like
cout << monster_waypoint[3][1].x;
it works fine. I assure there is nothing wrong with the binary file itself.
Advertisement
The C++ IOStream format operators do not work well with "binary" files.

Try either storing your data in text files, or try using the IOStream read() function.

Stephen M. Webb
Professional Free Software Developer

You're not supposed to use the >> operator for binary access. Use read() instead.

BTW, I don't understand why you are even bothering with p. When you read data from the file, the file advances it's internal position automatically.

It might also help if you post more code. There might be some problems with the way you are initializing things.
shouldn't your loop be

  for (n=0;n<monster_number;n++)  {	         for (N=0;N<5;N++)     {     }  }
Quote:Original post by Sigutis
I assure there is nothing wrong with the binary file itself.
This isn't directly an answer to what you were asking, but doing binary I/O with std::iostreams is really asking for trouble. They are designed to work with textual data, and even setting ios::binary does not do what you might think.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

->Bregma
Using the "read" function is what I though. I'll try it .

->Gage64
Yes, I can't remember why I started using them "p" in the first place

->nhatkthanh
Not really. Thats how I do it. Must be my twisted brain. I am thinking backwards or something :>.

->swiftcoder
I'll keep that in mind

Quote:Original post by Sigutis
->nhatkthanh
Not really. Thats how I do it. Must be my twisted brain. I am thinking backwards or something :>.


Each monster has waypoints, so the natural OO way of doing things is to add the waypoint data (array of 5 positions) as a data member of the Monster. Then when you iterate over monsters to .update() them, the update for a given Monster looks at its own waypoints.

How are you representing a waypoint, anyway? How big are you expecting the .x and .y fields to be, and why?
I do not have the monster class yet.
struct koords{	unsigned x,y;};

To represent the coordinates.

koords *monster_waypoint[4];

My map class member.

After loading monster_number:
	monster_waypoint[0] = new koords[monster_number];	monster_waypoint[1] = new koords[monster_number];	monster_waypoint[2] = new koords[monster_number];	monster_waypoint[3] = new koords[monster_number];	monster_waypoint[4] = new koords[monster_number];


And so I store all monster first waypoint in monster_waypoint[0]
Second in monster_waypoint[1] etc.
I can access the x positions of 1st monster :
monster_waypoint[0][0].x
monster_waypoint[1][0].x
monster_waypoint[2][0].x
monster_waypoint[3][0].x
monster_waypoint[4][0].x


x and y are unsigned integers but I give 10 symbol space fields in the files for each number.
I suspect you are confused about several things. Could we see your data file?
since you're in control of serialization.. save yourself the heartache.

This topic is closed to new replies.

Advertisement