Jump to content
  • Advertisement
Sign in to follow this  
Bykolak

[C++] Can`t load map from file using getline

This topic is 2710 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

Hi im writing tile-based game and i have problem with loading tile ID from file. Here`s the 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].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].type*50,0,t*50,i*50+15,50,50);
}
}



map.txt file1 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


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




Problem is that program shows map full of zeros.

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites

Did you mean to do
map[t].type*50

???


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.




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 :/

(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 this post


Link to post
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:

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);
}

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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!