0

Help please, I wrote a script in C++ for loading a map file, parsing it with XML, and outputting the objects in the file. The XML parser works great, but it won't open the file. I have it exiting if the file doesn't exist, and the file doesn't load, even though it's in the same folder... any suggestions?

void Map::LoadMap(const std::string& filename) {
	std::ifstream file;

	if(!file.is_open()) {
		std::cout << "Loading the map file " << filename << " failed..." << std::endl;
		return;
	}

	Xml xml;
	std::string s;

	while(!file.eof()) {
		s += file.get();
	}

	xml.Parse(s);
	if(xml.GetRoot()->GetType() != "rpgfile") {
		std::cout << "Loading the map file " << filename << " failed because the file isn't the correct type..." << std::endl;
	}

	std::string version = xml.GetRoot()->GetProperty("stickmin");
	std::map<int, MapPoint*> pointref;

	for(int i = 0; i < xml.GetRoot()->GetNodeChildren(); ++i) {
		Xml::Node* node = xml.GetRoot()->GetChild(i);

		if(node->GetType() == "rock") {
			std::cout << "Rock placed." << std::endl;
		} else if(node->GetType() == "grass") {
			std::cout << "Grass placed." << std::endl;
		}
	}

	file.close();
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by thelamb
0
void Map::LoadMap(const std::string& filename) {
	std::ifstream file;
 
	if(!file.is_open()) {

is_open() will _always_ return false, no matter what 'filename' you hand over to this LoadMap function.
Think about what you're missing, how will ifstream 'know' which file to open?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.