Jump to content
  • Advertisement
Sign in to follow this  
Sigutis

C++ mysticism while reading binary

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
->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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!