loading/filename problem

Started by
1 comment, last by Bezzant 22 years, 6 months ago
quick question... im in the process of writing what would seem like a text engine for a text adventure or whatever it gets turned into (if anything) i can load files ok, but just ran into a small problem.... i got a struct for PLAYER that stores player info and his/her current x and y position on the map (an array)... well when they move i need to load a file, this file needs to match where they are on the map.... so i thought that the easiet way to go about that would be creating files that have file names like this: 1.2.dat where the 1 and the 2 are the x and y pos on the map for where they load thus, when the player moves north, i update their position on the map and then want to load the file at there new position... so basically, PLAYER.X"."PLAYER.Y".dat" BUT, heres my file loader
  
void LoadMapArea(void)
{
	char ch;
	int mode = ios::in;
	char AreaFilename[20]= //problem here ;

	fstream fin(AreaFilename, mode); //input file

	while(fin.get(ch)) //read ch

{
	cout<<ch;          // print ch

}
	fin.close();       //close file

}
  
this line here is the problem: char AreaFilename[20]= well, i need to get it to = PLAYER.X.PLAYER.Y.dat.... but whatever i try i get errors because it cant convert int to char. so i need an alternative method... any help would be great, thanks for your time! Alan
Advertisement
Here a quick and dirty solution:
  char AreaFilename[20];sprintf (AreaFilename,"%u.%u.dat",PLAYER.X,PLAYER.Y);  

The format string of sprintf is the same as the one of printf. When using it this way, you must ensure that your string buffer is large enough.
thanks, that works great!

thank you

This topic is closed to new replies.

Advertisement