Return value

Hello!

I was just "doodling" around with my xml parser.

And wanted just to build some error report, incase the datafile is missing.

Reading the info about TinyXML it says that LoadFile should return true if it went OK.
So i assume it returns false if not

This code however does not seem to work:


1
2
3
4
5
6
7
8
9
10

bool status;

status = doc.LoadFile();

if (status == false){
cout << "False" << endl;
return -1
}


It just stops.
Any idea?

Last edited on
If you mean TinyXML-2, then LoadFile returns an XMLError enum:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
enum XMLError {
     XML_SUCCESS = 0,
     XML_NO_ATTRIBUTE,
     XML_WRONG_ATTRIBUTE_TYPE,
     XML_ERROR_FILE_NOT_FOUND,
     XML_ERROR_FILE_COULD_NOT_BE_OPENED,
     XML_ERROR_FILE_READ_ERROR,
     UNUSED_XML_ERROR_ELEMENT_MISMATCH,  // remove at next major version
     XML_ERROR_PARSING_ELEMENT,
     XML_ERROR_PARSING_ATTRIBUTE,
     UNUSED_XML_ERROR_IDENTIFYING_TAG,   // remove at next major version
     XML_ERROR_PARSING_TEXT,
     XML_ERROR_PARSING_CDATA,
     XML_ERROR_PARSING_COMMENT,
     XML_ERROR_PARSING_DECLARATION,
     XML_ERROR_PARSING_UNKNOWN,
     XML_ERROR_EMPTY_DOCUMENT,
     XML_ERROR_MISMATCHED_ELEMENT,
     XML_ERROR_PARSING,
     XML_CAN_NOT_CONVERT_TEXT,
     XML_NO_TEXT_NODE,
 
     XML_ERROR_COUNT
 };


Note that XML_SUCCESS is 0, which would be "false" in a boolean context, the opposite of what you are assuming.

You need to do something like this:
1
2
3
4
5
XMLError status = doc.LoadFile("filename");
if (status != XML_SUCCESS) {
    cout << "Failed\n";
    return EXIT_FAILURE;
}

Last edited on
Thanks for your help!

I did not find the information about the enum!

Just did find this: http://www.grinninglizard.com/tinyxmldocs/classTiXmlDocument.html

However, It dont seem to be able to just adapt the enum variant.
Probably since i have not been using it before. And i get declaration errors.
"XMLError not declared in this scope"
"XML_SUCCESS not declared in this scope"
I said if you are using TinyXML-2, then LoadFile returns XMLError. If you are just using TinyXml, then it supposedly returns a bool. I just assumed you might have been using 2 but reading docs for 1.

Apparently you are using the old version, so I have no idea what the problem is. The "code" you show is obviously fake, however, so showing some real code (preferably a small complete program) would help.

And remember that code tags use square brackets (not angle brackets), i.e., [code] [/code]
Last edited on
Thanks again.

Yes sorry, i should have checked my version first.
Its just a Raspberry Pi, and i used whatever Debian package manager pulled in.

I am embarrassed to say that i did not have any fault after some investigation.

I was in my backup folder when i compiled/run, but i was editing in the work folder.
I am very sorry for taking your time with this.

Actually the code snippet is not "fake" in that sense. It is actually the right one, but i did not include all the parsing functionality.

Edit:
By the way.

What would you say would be best way when doing these kind of status checks.
Just initialize a bool in the beginning, and "re-use" it for every function call that you wan to status-check.

What way would you think is the best?
Last edited on
I am very sorry for taking your time with this.

No problem. :-)

Actually the code snippet is not "fake" in that sense.

I should have said "incomplete", not "fake".
It's always best to have a runnable program.
Next time!
Thanks!

Did you catch my late edit? :)
For checking the status you often don't need a variable:
1
2
3
4
if (!doc.LoadFile()) {
    cout << "Error loading file\n";
    return -1
}

But if you do need one, a single status variable would be okay.
Topic archived. No new replies allowed.