Jump to content
  • Advertisement
Sign in to follow this  

how load map got start?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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);
// 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
// do this if you want to store the numbers instead
// result.push_back(x - '0');
// 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,

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!