Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if QTextStream::operator>> failed

Tags:

c++

qt

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?
like image 439
sashoalm Avatar asked Nov 29 '25 20:11

sashoalm


1 Answers

You can query the stream's status after reading by calling stream.status():

if (stream.status() == QTextStream::Ok) 
{
    // succeeded
} 
else
{
    // failed
}
like image 198
Armen Tsirunyan Avatar answered Dec 02 '25 09:12

Armen Tsirunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!