TinyXml LoadFile Problem

Started by
6 comments, last by dilyan_rusev 13 years, 5 months ago
I am currently writing some test programs to get a better handle on TinyXml, as my group in school is planning on using it for a multi-semester project.

I wrote some functions that write out to a xml file no problem. The functions in it are a bit funky, but I am beginning to wrap my head around it. The problem I am having is when I go to open a xml file and read form it.

 void getXml(Fighter &inFighter, const char* filename){	TiXmlDocument doc(filename);	bool loadOkay = doc.LoadFile();	if (loadOkay) { ... }	return;}


The call:
	char* filename = "fighter_output2.xml";	getXml(fighterTwo, filename);


The filename is correct (I copy/pasted from Explorer), and is in the same directory as the project, which is where the xml I output earlier in the program is dropped off. LoadFile is failing and returning an error. I have googled and found at least 10 programs that use this exact same syntax, so I cannot figure out where I am going wrong. Is there a VS2008 setting I need to change, or an additional #include (beyond "tinyxml.h") required to access these files?

Any help would be much appreciated, thanks.
Advertisement
Perhaps you are not executing from the current directory? Try running the exe from Explorer. You can set the Working Directory by using Project Settings to set the Configuration Properties, Debugging, "Working Directory".
Try using the full path to the file, instead of just the filename. If that works, then you know it's a problem with your current working directly, otherwise it's something else.
I directed it to an easy to find location (c:/xml/) in the code, and it found the document no problem. I then deleted that segment and ran again the original way, and it worked first try. I can't explain why it works now but not last night.

Regardless, I won't complain, thank you both for the help.
When you run from the IDE, you are rarely running from the directory you expect. Check your project settings.
I'm using VS2008, and the default Working Directory states that it is the one where the project resides. So I was putting the .xml I wanted to read in the same folder as the .vcproj file. It works today, but did not last night, no idea why.

I have added "Setup VS to work with multiple directories and builds" to my list of goals for this self-guided project. I remember Game Coding Complete having a good breakdown of the author's method for it, so I will go back and re-read that. We scratched the surface on IDE setup in one of my classes, but I think I am going to have to learn a lot more about it for this much larger undertaking.

Thanks again for the input.
If you intend on having the program open a file in its installation directory consider using GetModuleFileName to get the path to your executable otherwise you may run into other problems - e.g. when launching an app from the start menu the current directory will not be the one your executable is in and your open will fail.
There is a very nice thing in visual studio (I am not 100% certain if it is available in c++).

Add the xml file to your project. In the solution explorer, find the file and right-click->properties. Let Build action be None and set Copy to output directory to Copy always.

Visual studio should make certain that your file is copied in your build directory, and you wouldn't care about this anymore :)

Edit:
Now I see it doesn't. You can create a custom build step and a tool that does this automatically for you. Or use absolute paths for debugging and relative for release builds

Edit 2:
stevenmarky's solution is best IMO

[Edited by - dilyan_rusev on October 26, 2010 11:39:50 AM]

This topic is closed to new replies.

Advertisement