# [C++] Cant load map from file using getline

## Recommended Posts

Hi im writing tile-based game and i have problem with loading tile ID from file. Heres the code.

[code]const int MAP_X=10, MAP_Y=10;

main()

{

CTile map[MAP_X][MAP_Y]; // CTile is a class that contain all info about tile
std::ifstream file("map.txt");
if (file.is_open())
{
while (file.good())
{
int i,t;
for (i=0; i<MAP_Y; i++)
for (t=0; t<MAP_X; t++)
{
std::string line;
std::getline(file,line,' ');//should stop getting data when there is a space " " in file
map[t][i].type=atoi(line.c_str()); // convert string and assign to array

}

}
file.close();

for (i=0; i<MAP_Y; i++)
for (t=0; t<MAP_X; t++)
{
blit(tile,buffer,map[t][i].type*50,0,t*50,i*50+15,50,50);
}
}

[/code]

map.txt file[code]1 1 0 1 0 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 0 2 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
[/code]

p.s there is a space at the end of each line.

Problem is that program shows map full of zeros.

##### Share on other sites
Did you mean to do
[code]map[t][i].type*50[/code]

???

##### Share on other sites
Try adding some basic error checks:

if (file.is_open()) {
......
} else {
std::cout << "Couldn't open file";
}
file.close();

for example.

If you are opening the file successfully you could use your debugger to step through the code line by line to see what actually happens).

(There are obviously alot of code missing from what you've posted)

##### Share on other sites
[quote name='yewbie' timestamp='1310505430' post='4834509']
Did you mean to do
[code]map[t][i].type*50[/code]

???
[/quote]

Yes, because .type is connected with my bmp file that contains all the tiles and the tile size is 50. So each value reffers to different place in bmp file.

blit() function is taken from allegro.

[quote]Try adding some basic error checks:[/quote]

I added allegro_message("error loading file"); as You suggested but it turns out the file is opened correctly and if i try to debug my program screen freezes and i have to ctrl+alt+del him :/

[quote](There are obviously alot of code missing from what you've posted)[/quote]

Not too much and there is no chance that map[x][y].type changes because of other functions. It is used only in loading from file and showing it on screen.

##### Share on other sites
atoi() returns "0" when it cannot convert the string to an integer. You may notice that it is hard to tell this error code from the domain of valid integers!

One alternative is to use the stream extraction operators, like so:
[code]
const int MAP_X=10, MAP_Y=10;

int main()
{
CTile map[MAP_X][MAP_Y];
std::ifstream file("map.txt");

for(int x = 0 ; x < MAP_X ; ++x)
{
for(int y = 0 ; y < MAP_Y ; ++y)
{
if(!(file >> map[x][y]))
{
// Error handling
}
}
}

render(map);
}
[/code]
This doesn't try very hard to match the structure of the file (it would be perfectly happy if you put all the numbers on a single line). It is a start.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627740
• Total Posts
2978884

• 10
• 10
• 21
• 14
• 14