Hi!
I am trying to load the information in a file and put into an array. Can anyone tell me how I might do this? Also, how do start reading from a different line in a file? I am using fstream.h to do this. Thanks! ^_^
Hi!
I am trying to load the information in a file and put into an array. Can anyone tell me how I might do this? Also, how do start reading from a different line in a file? I am using fstream.h to do this. Thanks! ^_^
Thanks for the getline suggestion. My file will contain lines of numbers. I'm making a map file loading engine.
Someone please help!!!
I can help, I've done something like this before. Okay, let's say this the file (a 10x10 square):
1 = grass
2 = water
3 = mountain
2 2 2 2 2 2 2 2 2 2
2 2 1 1 1 1 3 1 2 2
2 1 1 1 3 3 1 3 1 2
2 2 1 3 1 1 3 1 1 2
2 2 2 1 1 1 3 1 2 2
2 2 1 1 3 2 2 1 1 2
2 1 1 3 3 2 1 3 2 2
2 2 2 1 3 1 3 1 1 2
2 2 2 2 2 1 1 2 2 2
2 2 2 2 2 2 2 2 2 2
This would require an array of 100 elements.
To initialize this, let's say you have a function called InitMap().Code:int islandMap[10][10];
Or something along those lines. I hope that helps a little. Let me know if that still won't work, I've another idea that may.Code:void InitMap() { ifstream map("island.map"); char ch; for (int i=0; i<10; i++) { for (int j=0; j<10; j++) { map >> ch; islandMap[i][j] = (int *)ch; } } }
Brendan
Oops. In the above InitMap() function, where it says:
Take out the *. It will work then. So it should look like this:Code:islandMap[i][j] = (int *)ch;
BrendanCode:islandMap[i][j] = (int)ch;
Lol oops actually none of that will work. Here, this works, and I know this because I tested it:
A DispMap() function would look something like this:Code:void InitMap() { char str[100]; ifstream map("island.map"); for(int i=0;i<=10;i++) { map>>str; for(int j=0;j<10;j++) { switch ((int)str[j]) { case '1': islandMap[i][j]=1; break; case '2': islandMap[i][j]=2; break; case '3': islandMap[i][j]=3; } } } }
Hope that helps!Code:void DispMap() { for (int i=0; i<10; i++) { for (int j=0; j<10; j++) { switch (islandMap[i][j]) { case 1: cout << "-"; break; case 2: cout << "~"; break; case 3: cout << "^"; break; } } cout << "\n"; } }
Brendan