• 12
• 12
• 9
• 10
• 13

# TinyXML Help!

This topic is 2624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

I'm having a problem with the TinyXML reader. I've imported the relevant tinyXML files into my project in VS2008 and figured I'd simply start by creating a TiXmlDocument based on the example xml file I've got. So I used the code:

[size="1"] string file = "Level1.xml" TiXmlDocument doc(file.c_str()); if ( doc.LoadFile() ) // Do Stuff else // Do other stuff [size="1"]

However the program will never enter the body of the code for the if statement, meaning there was a problem loading the file. After looking at the LoadFile() method I saw it called the TiXmlFOpen() method, which you can see below:

[size="1"][size="1"][color="#008000"] FILE* TiXmlFOpen( const char* filename, const char* mode ) { #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) FILE* fp = 0; errno_t err = fopen_s( &fp, filename, mode ); if ( !err && fp ) return fp; return 0; #else return fopen( filename, mode ); #endif 

[color="#000000"]Here the variable 'errno_t err' was set to 2 and LoadFile() returned false.

I'm not really sure what else I can do to get TinyXML to read from my XML file! If I could do that I'm sure I could play around with the resulting document and work out how to use the rest of the program, I just find its difficult in loading a simple .xml from inside the debug folder a little strange.

-- Senor Beer

N.B. Here is the example xml im using, just in case it helps:

 <?xml version="1.0" ?> <shapes> <circle name="int-based" x="20" y="30" r="50" /> <point name="float-based" x="3.5" y="52.1" /> </shapes> 

##### Share on other sites
My guess is that your file example.xml is not in the same directory as your executable. If this is the case, then it would be the reason for the failure of opening the file. Say your executable has the following path: "C:\Example\Debug\Program.exe" and your XML file has: "C:\Example\example.xml". In order to open this file correctly you would have to call the LoadFile member function with the relative file name "..\example.xml" or the absolute file name as shown above.

Furthermore you can look up the documentation for fopen_s error codes, and decipher the error code of 2 pretty easily. Reading directly from the documentation, an error value of 2 signifies "No such file or directory."

Also, if you are running your program from your IDE, it may set a different base path than you may assume. I'm almost positive you can change the default base path for Visual Studio to use in the configuration properties somewhere, but don't quote me on that.

##### Share on other sites

[size="1"][size="1"][color="#000000"]I just find its difficult in loading a simple .xml from inside the debug folder a little strange.

Usually, by default, Visual Studio sets the project folder as your working directory. So to load a file from the debug folder in debug mode, you probably have to change your code to open "Debug/Level1.xml" (or whatever your debug bin folder is). Alternatively, you can move the file itself to the root project directory.

##### Share on other sites

Furthermore you can look up the documentation for fopen_s error codes, and decipher the error code of 2 pretty easily. Reading directly from the documentation, an error value of 2 signifies "No such file or directory."

You were right, the file path was the issue but the solution wasn't what i expected. On my friends machine (we're using SVN) all he needed was to change the file path to:[size="1"]TiXmlDocument doc([size="1"][color="#a31515"][size="1"][color="#a31515"]"..\\Debug\\Level1.xml"[size="1"]);

But on my machine it has to be:

[size="1"]TiXmlDocument doc([size="1"][color="#a31515"][size="1"][color="#a31515"]"L:\\Team Project\\Marionette\\Debug\\Level1.xml"[size="1"]);

Which is just...bizarre! It's working in any case but if anybody has any insight into why my filepath must be different that would be great!

- Senor Beer

##### Share on other sites

Alternatively, you can move the file itself to the root project directory.

Or, ideally, have the program figure out where the .exe is stored and not depend on the working directory == the program directory (i.e. running from a shortcut on the desktop).

Note: probably not the best solution ever, but it works.
 std::wstring getBinPath() { std::wstring binPath; wchar_t exePath[MAX_PATH]; GetModuleFileNameW(NULL, exePath, MAX_PATH); binPath = exePath; binPath.erase(binPath.find_last_of(L"\\/") + 1); return binPath; }