Loading an array from a file

Started by
5 comments, last by Zahlman 18 years, 10 months ago
Alright, for some reason I like doing games in ASCII. Maybe it's because I'm a terrible artist. But I want to make "map" files and load them. I was thinking to use a 2d array to do it. But I don't know how I would go about doing that. I guess it should be in binary mode. But beyond that, I'm lost. Edit: C++ - win32 console
skulldrudgery--A tricky bit of toil
Advertisement
It can be done in text mode too.


save

(store x and y dimensions of map on line 1)
(y lines of x length)

load
(read array dimension)
(size array x by y)
(load each array indice one at a time, y * x times)
So do I just use ofstream?


char map[height][width];ofstream mout( "map.txt" );mout << height << width << map;


Edit: Doraahaahhhh! Typos...
skulldrudgery--A tricky bit of toil
The array can't just be fed as-is to a stream, and you may find a need to manually insert whitespace at some points (can't think right now :s) but you have the right basic idea. To output the array you'll need to iterate over it and output the characters one at a time:

for (int i = 0; i < height; ++i) {  for (int j = 0; j < width; ++j) {    mout << map[j];  }}


There are nicer and "cooler" ways to do this, but I am presenting what should be most obvious and conceptually easiest.
this is what i am doing for the maps in my zelda clone. and it works well, just don't forget to output a space after every array entry output.
Charles Reed, CEO of CJWR Software LLC
Ok, I got it.

ofstream mout( "map.txt" );mout << height << '\0' << width << endl; // not necessary?for ( int i = 0; i < height; i++ ) {        for ( int j = 0; j < width; j++ )        mout << map[ j ] &lt;&lt; '\<span class="cpp-number">0</span>';<br>    <br>    mout &lt;&lt; endl;<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>Thanks for the help.<br><br>BTW if I use this as part of a class called "Screen" for example. And I use const ints for height and width, should they be global variables or private class members?
skulldrudgery--A tricky bit of toil
Edit: Warning! Outputting '\0' to a text file (i.e. one intended to be read back in with the stream extraction >> operator, and/or viewed/edited in a text editor) is a bad idea. You almost certainly want to output ' ' (or perhaps a newline) instead.

Consider private, static class members.

However, if it's intended that there is logically only one Screen, and you are pretty confident of this part of your design, consider desigining it as a namespace rather than a class. You can make use of the C++ linking model, with a bit of care, to "hide" namespace-global data and helper functions as desired, the same way you'd use private in a class. You will also save the extra code associated with maintaining/caching/passing around (an) object instance(s), as well as any extra code you might already be contemplating for a Singleton implementation. :) (Of course, this comes at the cost of flexibility; significant reworking of the system is required if it becomes necessary to make actual Screen objects. But you know, it's never happened to me...)

This topic is closed to new replies.

Advertisement