TinyXML Help!

Started by
3 comments, last by Firestryke31 13 years, 3 months ago
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"]<code><br /> <br /> FILE* TiXmlFOpen( const char* filename, const char* mode )<br /> <br /> {<br /> #if defined(_MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1400 )<br /> <br /> FILE* fp = 0;<br /> <br /> errno_t err = fopen_s( &amp;fp, filename, mode );<br /> <br /> if ( &#33;err &amp;&amp; fp )<br /> <br /> return fp;<br /> <br /> return 0;<br /> <br /> #else<br /> <br /> return fopen( filename, mode );<br /> <br /> #endif<br /> </code><br /> <br /> Here the variable &#39;errno_t err&#39; was set to 2 and LoadFile() returned false. <br /> <br /> I&#39;m not really sure what else I can do to get TinyXML to read from my XML file&#33; If I could do that I&#39;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.<br /> <br /> Thank you in advance&#33;&#33;<br /> – Senor Beer<br /> <br /> N.B. Here is the example xml im using, just in case it helps:<br /> <br /> <br /> <br /> <code> <br /> <br /> &lt;?xml version=&quot;1.0&quot; ?&gt;<br /> <br /> &lt;shapes&gt;<br /> <br /> &lt;circle name=&quot;int-based&quot; x=&quot;20&quot; y=&quot;30&quot; r=&quot;50&quot; /&gt;<br /> <br /> &lt;point name=&quot;float-based&quot; x=&quot;3.5&quot; y=&quot;52.1&quot; /&gt;<br /> <br /> &lt;/shapes&gt;<br /> <br /> </code><br /> <br />
Advertisement
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.
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume

[size="1"][size="1"]I just find its difficult in loading a simple .xml from inside the debug folder a little strange.</blockquote><br /> 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 &quot;Debug/Level1.xml&quot; (or whatever your debug bin folder is). Alternatively, you can move the file itself to the root project directory.

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"][size=&quot;1&quot;]&quot;..&#092;&#092;Debug&#092;&#092;Level1.xml&quot;[size=&quot;1&quot;]);<br /> <br /> But on my machine it has to be:<br /> <br /> [size=&quot;1&quot;]TiXmlDocument doc([size=&quot;1&quot;][size=&quot;1&quot;]&quot;L:&#092;&#092;Team Project&#092;&#092;Marionette&#092;&#092;Debug&#092;&#092;Level1.xml&quot;[size=&quot;1&quot;]);<br /> <br /> Which is just…bizarre&#33; It&#39;s working in any case but if anybody has any insight into why my filepath must be different that would be great&#33;<br /> <br /> Thanks for your help&#33;<br /> - Senor Beer

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

This topic is closed to new replies.

Advertisement