A File I/O Question

Started by
13 comments, last by PepsiPlease 21 years, 9 months ago
Hey, Currently, I am working on a 3D tile-based game. At the moment, I have my map stored in an array like so:

1111111111
1000000101
1111001101
1001011001
1101010011
1111001111
1000110001
1000111101
1110011011
1111111111

note: it is actually in the proper array format (char map[10][10] = {{1, 1, 1, 1 ........
 
Now, a problem is developing. I want to store my array in a text file, so I tried that, but it wouldn''t store it in an array, just read the numbers. My question is this: how do I read a set of numbers in a text file and store them in an array? Thanks
Advertisement
Hello,
Try this...

  ifstream infile("mapdata.txt",ios::in);//substitute your file name for "mapdata.txt"if(infile == NULL)//error,file did not open...   infile.close();//file opened correctly...for(int x=0;x<10;x++)   for(int y=0;y<10;y++)      infile>>array2d[x][y];infile.close();  

also, i noticed you were using char's instead of int's. Try switching your array to integers instead.
good luck

[edited by - shdaga on June 29, 2002 3:36:07 AM]

[edited by - shdaga on June 29, 2002 3:36:52 AM]
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>
Thanks a lot!

I will be trying that now...
Youll have to be a little more careful actually.
If your map is not always of same size, you will need to either write the size of the map first in the file, or before reading seek to the end and store that position so you know how large the file is(and thus how big a map it would make). Then you allocate the array with new using the information youve gained.

Also id suggest that you read/write in binary mode(use the ios::binary flag). You then use the write/read methods of the fstream object to read/write the array to/from file.

Btw, using chars will work just fine as long as you are okay with only having 256 different tiles(-127 to 128) you might want to use an unsigned char instead though(0-255), because it makes more sense for numbering tiles.

If you want to read up on file I/O check
http://www.cplusplus.com/ref/#libs under iostream library

C/C++ Runtime library reference|Boost utility libraries|SGI STL Reference

[edited by - ziphnor on June 29, 2002 4:20:10 AM]
Well, actually all my maps are the same size, so do I still need to do all that or will what I am doing be fine?

Also, what''s the difference whether I do binary mode or not?

You dont have to worry about dynamic allocation if all the maps are of the same size.(So the example given earlier will work just fine).

As to writing in binary vs. in text mode:
If you write the number 1 in text mode, what will ACTUALLY be written is the character code for the number 1.
If you write in binary mode the number 1 will be written as the binary representation of 1(ie 00000001 if your number is a byte).

Binary representation have some advantages in that you can write data directly from a file into the memory location and use it right away.(ie you could just use the read method of the fstream object, hand it your array pointer, tell it how much to read and it would write the whole thing from the file directly into memory).

heres how I do mine :
my .map file is simply a txt file that looks like that
1 1 1 1 1
1 0 0 0 1
1 0 0 0 0 and so on, you can put any number in there so theres no
limit to what your map file can do. IE 255 could be a town tile that teleports you to a specific city etc...


ifstream worldmap;
worldmap.open("map2.map");

if(worldmap.fail())
cout << "Error : Could not find map2.txt";

int number;

for(int y= 0; y < MAP_SIZE_Y; ++y)
{
for(int x= 0; x < MAP_SIZE_X; ++x)
{
worldmap >> number;
map[x][y]= number;
}

worldmap.get();

}

worldmap.close();



In Construction : http://www.gdev.org
Again, the above text file examples will work just fine, and for such small datastructure as a 10x10 array it wont be a problem, on the other hand if you are for example storing a 1000x1000 char array then it might come in handy to read the whole array with one read(for performance reasons).
Also should the array be something other than a char(ie single byte) array then binary mode makes a whole lot more sense.
Hello peeps,
Just to play the opposite of Ziphnor, one of the main reasons you can write to your map files in text mode is so you can easily see what your map may physically look like. It makes editing or creating small maps easier. But I do agree with Ziphnor, as soon as you have another way of creating your maps, i.e. not using notepad and just plugging in numbers, go ahead and store them in binary format. Also, look into making your maps dynamic, it will help in the long run, when you finally do decide to make arbitrarily sized maps.
Here's a new dance everyone can do, do the Kirby:<(*.*<) <(^.^)> (>*.*)> <(^.^)> <(*.*<) (>^.^)>
If these numbers are all 0s and 1s, it''s a better idea to use bools or chars to make your program a little bit more efficient. I''m an efficiency freak :D.

This topic is closed to new replies.

Advertisement