Archived

This topic is now archived and is closed to further replies.

ZadrraS

[C++|SDL] I need to load level data from text file.

Recommended Posts

I never learned file i/o , i simply need to make my char map[17][20] equal to whats in a text file. How do i do it?

Share this post


Link to post
Share on other sites
I assume you are using C++. The necessary steps are quite simple:
- open the map file
- read rows
- finito
Heres a bare bone example without sophisticated error checking:
map.txt

xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx


#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])
{
char map[20][17]; // the map

ifstream MapFile("map.txt"); // open the file map.txt

if (MapFile) // file opened succesfully

{
// start reading 20 rows

for (int height = 0; height < 20; ++height)
{
// read current row int map array

MapFile.read(map[height], 17);

// print current row just to check

for (int width = 0; width < 17; ++width)
{
std::cout << map[height][width];
}
}
std::cout << std::endl;
}
std::cin.get();

return EXIT_SUCCESS;
}



[edited by - baumep on April 22, 2004 12:40:50 PM]

Share this post


Link to post
Share on other sites
There are lots of tuts out there for processing text files, but take a look at tutorial #10 on Nehe. Deals specifically with what you''re asking.

Share this post


Link to post
Share on other sites
its really easy. just as using cout or cin in my opinion. you need to use file streams.

step 1 ) #include <fstream>

step 2 ) declare an ifstream object. ifstream stands for input file stream, and its a class that can take input from a file. so first thing you need is to declare an object of this class. something like

ifstream mystream;

step 3) open the txt file. this is easy, just do:

mystream.open("MyMap.txt");

step4) read in the file data:

i dont know what your map txt file looks like. is it a bunch of columns and rows? is it just one big row? this is what my code looks like to read in the map data into my map[][] array:

// Error Check

if(!file_in)
return;

for(int y = 0; y < 19; y++)
{
for(int x = 0; x < 25; x++)
{
file_in >> map[y][x];
}

}


step 5) we are done! now we have to close the txt file. just do

mystream.close();

thats it! hope i helped...

Donkey Punch Productions.net

Share this post


Link to post
Share on other sites
Hmmm, it doesn''t work. What could be wrong?

void LoadMap() {
ifstream MapFile("map.txt");

for(int y = 0; y < 17; y++) {
for(int x = 0; x < 25; x++) {
MapFile >> map[y][x];
}
}

MapFile.close();
}

Share this post


Link to post
Share on other sites
are you trying to write to a file or read from a file? use fstream instead of ifstream or ofstream instead of ifstream

Share this post


Link to post
Share on other sites
Im trying to fill my map array from numbers in a file.
I tried using fstrem, but i get an error

error C2664: ''__thiscall fstream::fstream(int)'' : cannot convert parameter 1 from ''char [8]'' to ''int''

Share this post


Link to post
Share on other sites
How is your map declared?
Are you including <fstream> of <fstream.h>?
Btw. for your exapmle to work the map file must have all elements separated by blanks, e.g.:

1 2 3 4 4 4 3 1

because the stream operator >> will read all data until the next blank.
Another issue is if your map is of type int all the elements in the MapFile must be ints because the stream operator will try to convert to the maptype.
Try to compile my first example and see if you can modify it to meet yout needs.

Share this post


Link to post
Share on other sites
Wow, thats all i was missing, spaces! Now it works.
Bu i didn''t really understand your way of doing it baumep so ive done it the simpler way- graveyard fillas way. And now it works. Thanks everyone!

Share this post


Link to post
Share on other sites
no no no no no! Thats not right... something is wrong. When i, for example do this:

0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 2 0 0 0 0 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 0 2 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

i tried switching :
for(int y = 0; y < 17; y++) {
for(int x = 0; x < 25; x++) {
in the map loading functions and then it does everything right, just that the rows of blue (1) and green (2) tiles go from right to left, not from up to down.

Share this post


Link to post
Share on other sites
oh damn, how stupid could i get, sorry everyone. There were not the right amount of variables in the text file. Everything works, let this post die

Share this post


Link to post
Share on other sites