fstream reading out of order?

Started by
8 comments, last by ext 18 years, 9 months ago
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
XBox 360 gamertag: templewulf feel free to add me!
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?
XBox 360 gamertag: templewulf feel free to add me!
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.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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.
skulldrudgery--A tricky bit of toil
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]

Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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?
XBox 360 gamertag: templewulf feel free to add me!
You can use the
and
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.
skulldrudgery--A tricky bit of toil
templewulf: read the forum FAQ.
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.

This topic is closed to new replies.

Advertisement