Jump to content
  • Advertisement
Sign in to follow this  
templewulf

fstream reading out of order?

This topic is 4831 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

Okay, I searched gamedev, google, cplusplus and a couple of my reference books, but now I'm stumped! I'm developing an engine for a 2d tile-based RPG as practice (I know there are way better engines out there!) In any case, under the genmap.h header, I have 2 member functions LoadMap() and SaveMap(). When I call SaveMap() and pass it a char* string, it saves the tilemap array in binary using fstream.write(). The values are correct as it is being passed to the file, but in LoadMap() they come up out of order! The code for the main project is at: http://home.earthlink.net/~foreverbonded/Genesis.zip The code for the quickie program to generate the map is at: http://home.earthlink.net/~foreverbonded/maptest.zip Also, how do you embed a snippet of code in the middle of a post? You know, it looks like an <iframe> with its own little scrolly-bar. Thanks in advance for the help

Share this post


Link to post
Share on other sites
Advertisement
Does anybody know about the way files are stored and retrieved with fstream? I checked my for loop that contains the blitting, and I think they're the same for both loading and saving; what could be the problem?

Share this post


Link to post
Share on other sites
use [ source ] and [/ source ] to isert code in your post

instead of using write() you could try using the insertion and extraction operators to work with the file e.g.#

File << MyInt << MyDouble;

later

File >> MyInt >> MyDouble;

Always works for me.

Share this post


Link to post
Share on other sites
You left out a sizeof() in the loading function. I think. I'm not too good with file I/O.

unsigned short nSize = tilemap[0][0][0];

should be:
unsigned short nSize = sizeof(tilemap[0][0][0]);

right? Or maybe I'm way off.

Share this post


Link to post
Share on other sites
As far as I can tell and although I'd have done things differently, the code in genmap.h is correct. Can you explain exactly how out-of-order do the tile get?

skulldrudgery is correct

And since you don't like hard-coding array dimensions, consider using a std::vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by skulldrudgery
You left out a sizeof() in the loading function. I think. I'm not too good with file I/O.

unsigned short nSize = tilemap[0][0][0];

should be:
unsigned short nSize = sizeof(tilemap[0][0][0]);

right? Or maybe I'm way off.


Thats right. Well spotted[grin]

Share this post


Link to post
Share on other sites
Ohhhhhhhhhh! Thanks, guys, that worked!

As to the insertion / extraction operators, I tried those, but for some reason the ios::binary flag never worked with them. They would always appear in notepad as ascii characters instead of looking nonsensical as they should under ascii representation.

I momentarily considered doing a text-parsing system for loading vars and such, (a la Linux), but this is just a practice project and I'm not entirely fond of such systems.

Anyways, thanks again!

P.S. How do you insert a reply into your text that has its own box around it?

Share this post


Link to post
Share on other sites
You can use the [ quote ] and [ /quote ] tags (minus the spaces) or if you want to quote another user's entire post, there is a "quote" button at the top right of their comment. Right next to the edit button.

Share this post


Link to post
Share on other sites
The shifting operators( >> and << ) split the types down in ascii characters and read a stream of such characters to build the type.
That's why you can see the values in a text editor.

For binary in- and output read() and write() are the way to go.
For a bigger project you should use a text format like XML to store your data and later write a translator for them to convert them to binarys, for the release version.

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!