how load map got start?

Started by
0 comments, last by darookie 19 years, 4 months ago
this is my code for loading a txt file but i dont know how 2 make it in a array ,or separate 1's from the 2's it show the map but thats it. int loadmap(void) { char x; int index=0; const char map1[] ="map.txt"; fstream file1(map1,ios::in);//open the file file1.seekg(0,ios::beg); file1.read(&x,1); while(file1.good()) { index++; file1.seekg(index); file1.read(&x,1); cout << x; } file1.close(); //close the file return 0; map.txt 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 http://www.newgameproject.net
Advertisement
If you want the result to be stored in an array (just the number values, not the colons) you'd best use a vector since you don't know the size of the map beforehand (at least not if you use the format as in your map.txt).
If you read the 1's and 2's bit by bit, you just check whether the value read is a colon or not:

// add to your include file list:#include <vector>using std::vector;// I replaced teh return value with a char vector.vector<char> loadmap(){   char x;   // the resulting char vector   vector<char> result;   // the index is not necessary, see below//   int index=0;   const char map1[] ="map.txt";   fstream file1(map1,ios::in);//open the file// you don't need to do this, the file pointer is already at the beginning//   file1.seekg(0,ios::beg);   file1.read(&x,1);   while(file1.good())   {     // you don#t need to seek since read() will move the file pointer for you//      index++;//      file1.seekg(index);      cout << x;      // here you check whether it's a number      if (x >= '0' && x <= '9') {         // do this if you want to store the literals         result.push_back(x);         // do this if you want to store the numbers instead//         result.push_back(x - '0');      }      file1.read(&x,1);   }   // you won#t need to do this manually; the file is closed automatically   //file1.close(); //close the file   return result;}

You can use the vector like a char array and obtain its size using the .size() member function.

Good luck,
Pat.

This topic is closed to new replies.

Advertisement