Archived

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

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

This topic is 4979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 on other sites
I assume you are using C++. The necessary steps are quite simple:
- open the map file
- finito
Heres a bare bone example without sophisticated error checking:
map.txt
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#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 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 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 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 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 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 on other sites
Does anyone know whats wrong with my code?

Share on other sites
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 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 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 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 00 0 0 0 1 0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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 on other sites
your y variable need only go to 20, correct? I think you have it going to 25.

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

• Forum Statistics

• Total Topics
628645
• Total Posts
2984028

• 9
• 9
• 9
• 10
• 21