Functions like sscanf return the number of successfully read items, which is useful for error checking, for example the code below would print "failed" because sscanf would return 3 (1, 2, 3 were read, but "text" was not a number).
Does QTextStream offer an equivalent way for error-checking?
const char *text = "1 2 3 text";
int a, b, c, d;
if (4 != sscanf(text, "%d %d %d %d", &a, &b, &c, &d))
printf("failed");
QString text2 = text;
QTextStream stream(&text2);
stream >> a >> b >> c >> d; // how do I know that d could not be assigned?
You can query the stream's status after reading by calling stream.status():
if (stream.status() == QTextStream::Ok)
{
// succeeded
}
else
{
// failed
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With