[C++|SDL] I need to load level data from text file.
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?
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
[edited by - baumep on April 22, 2004 12:40:50 PM]
- open the map file
- read rows
- 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]
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.
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:
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
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
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();}
are you trying to write to a file or read from a file? use fstream instead of ifstream or ofstream instead of ifstream
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''
I tried using fstrem, but i get an error
error C2664: ''__thiscall fstream::fstream(int)'' : cannot convert parameter 1 from ''char [8]'' to ''int''
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.:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement