I have some code for an XML parser that i am using for my game. I did not write the code myself, i got it from a friend who is helping me with my game. I have a problem with how it reads the file, it seems to overshoot the end of the file by about 10 characters. This is the read code (i wrote the code to strip comments ):
XMLNode *readXML(const char *file) {
FILE *in;
XMLNode *root;
char *data;
int nb;
fopen_s(&in, file,"rb");
if (!in)return 0;
fseek(in,0,2);
nb = ftell(in); //### Problem seems to be here
fseek(in,0,0);
data = new char[nb];
fread(data,1,nb,in);
fclose(in);
// Strip comments <!-- -->
... //### This code isn't part of the problem (i hope!)
nb = pos;
data[nb] = '\0';
root = new XMLNode("root");
populateObj(root,data,nb);
delete data;
return root;
}
It seems to set the size of the file larger than what it is. The result is that after fread, data is:
<mech>
<name>Test</name>
<maxHealth>100</maxHealth>
<img id=arm file=armImg.png/>
<img id=armMask file=armMask.png/>
<anim id=walkAnim file=walkAnim.png divX=50 divY=50/>
<anim id=walkMask file=walkMask.png/>
<anim id=jumpAnim file=jumpAnim.png/>
<anim id=jumpMask file=jumpMask.png/>
</mech>ýýýý««««««««þîþîþîþ
I should point out that i am using Visual Studio on WinXP.
Also, if i don't have the comments , i get a runtime error: "HEAP CORRUPTION DETECTED"
which i traced to the code delete data; (
that may be a result of my 'strip comments' code). EDIT: after further testing, i found that the problem is only on a certain XML file and i don't get the error if i remove comments from other files.
To see the whole document, see
here.
If someone could help me out here that would be great,
thanks.
[Edited by - XTAL256 on July 1, 2008 3:47:17 AM]