PDA

View Full Version : QTextStream operator>> and error detection



olidem
16th March 2009, 12:29
Hi!
I'm parsing integers from a QTextStream:

textstream >> x >> y ;

How can I check everything for correctness?


if( ! (textstream >> x >> y))
{
//...
}
does not work.

Thanks in Advance
Olli

olidem
17th March 2009, 08:25
<Push up>
If you parse numbers, how do you check for errors?

Or: do you check at all ? :D

Acce
27th July 2012, 09:18
Bump.

I see the latest post is from 2009, but I still don't see an answer how to do the error checking..

in the documentation it says it will set the variable to value 0 if it cannot read proper value ("If no number was detected on the stream", to be exact), which is not very good behaviour considering 0 is among the values I have to handle with this variable...

Do I really need to do some kind of idiotic work around?

wysota
27th July 2012, 09:26
QTextStream::status()

Acce
27th July 2012, 10:25
Thank you, I was looking at 4.7 reference apparently.

wysota
27th July 2012, 10:54
This method is available since Qt 4.1: http://doc.qt.nokia.com/4.1/qtextstream.html#status

Acce
27th July 2012, 11:17
Ok, I'm sorry for the mess.

This is the page I was looking at
http://doc.qt.nokia.com/4.7-snapshot/qtextstream.html#Status-enum
because it comes right up when googling "qtextstream reference", maybe the page has a mistake in it?
or did you mean that the error checking when reading has been there since 4.1? because I needed the WriteFailed
and I can't find it in 4.1...

Anyways thanks for pointing me to the right place.

EDIT:
I tried with both


int value;
stream >> value;
if(!(stream.status() == QTextStream::ReadCorruptData))
and


int value;
stream >> value;
if(!(stream.status() == QTextStream::WriteFailed))
and both result in behaviour I want when something else than integer comes from the stream.. what is the difference between these 2 statuses?

wysota
27th July 2012, 12:38
Ok, I'm sorry for the mess.

This is the page I was looking at
http://doc.qt.nokia.com/4.7-snapshot/qtextstream.html#Status-enum
because it comes right up when googling "qtextstream reference", maybe the page has a mistake in it?
What mistake? You are looking at the reference of an enum, not the method. At the end of the docs for the enum there is "See also status()" with a link to the method docs.


what is the difference between these 2 statuses?
I would guess that one is for read access and the other for write access.