Reputation: 145
Could somebody explain me why are these 2 types of return used?
int parse(QTextStream& out, const QString fileName) {
QDomDocument doc;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
out<<"Datei "<<fileName<<" nicht lesbar"<<endl;
> return 1;
}
QString errorStr;
int errorLine;
if (!doc.setContent(&file, false, &errorStr, &errorLine)) {
out<<"Fehler in Zeile "<<errorLine<<": "<<errorStr<<endl;
> return 2;
}
...
}
Here stays a part of another program.Why the code here doesn`t work the same way with
return 0;
?
int main(int argc, char *argv[])
{
QTextStream out(stdout);
out.setCodec("UTF-8");
if (argc != 3) {
out<<"Usage: "<<argv[0]<<" XML-Datei ist nicht vorhanden."<<endl;
return(1);
}
List wayList(out, argv[1]);
out<<"DOM-Baum gelesen."<<endl;
wayList.convert(argv[2]);
return 1;
}
Upvotes: 0
Views: 347
Reputation: 10998
In your first example, the function returns early to indicate an error. The file couldn't be opened so the function returns a value to the caller of that function. Couldn't set content, function returns a different value to the caller.
if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
out<<"Datei "<<fileName<<" nicht lesbar"<<endl;
return 1; // return value to caller
}
A function could for example call parse and check it's return value for success:
if ((parse(args...)) == 0) // success
In the end of function main()
, return 0;
indicates that the program ran successfully.
Upvotes: 3