PDA

View Full Version : Alternative to sscanf ??



Windsoarer
7th December 2008, 21:55
Dear Qt gurus,

I'm a newcomer to QT, trying to migrate an application written with MFC/VC++.
I'm stuck trying to read double values from a file ; in VC++ it was something like :



CString strong;
double x,y;
bool bRead = XFile.ReadString(strong);
int res = sscanf(strong, "%lf%lf", &x,&y);


How should it be done with Qt ???

Thanks

caduel
8th December 2008, 07:15
You can use >> to read floats from a std::istream or a QTextStream.
Another possibility is to split the string (tokenize it) and the convert the tokens with strtof or QString::toFloat().

Not sure if that is so much better that you should convert working code.

HTH

Windsoarer
8th December 2008, 19:43
Eventually, this is the way I did it



QByteArray textline;
const char *text;
textline = strong.toAscii();
text = textline.constData();
res = sscanf(text, "%lf %lf %lf", &x, &y, &z);


I'm just surprised it requires 3 to 5 lines in Qt instead of 1 in MFC for something which should be a very common operation.

Thanks for the hint anyway. I wasn't aware of the method toFloat(). No use here, though, there are three double values to read.

Cheers

wysota
8th December 2008, 19:51
First of all let's make one thing straight - sscanf() is C, not MFC :)
Second of all, you can use sscanf() in Qt as well.
Third of all you can do it properly ;)

QFile f("filename goes here");
f.open();
QTextStream str(&f);
double x,y,z;
str >> x >> y >> z;
Five lines including declaration and initialization of all variables which you happily ommited in "one line of MFC code".

Windsoarer
9th December 2008, 05:13
OK, forgive my ignorance. :confused:

What I meant is that sscanf takes a CString as an argument in MFC, but not a QString.
The method str >> x >> y >> z is also new to me, so thanks for the help.:)

Qt is great now that there is QtCreator... it just takes a little time to get familiar with the syntax.

wysota
9th December 2008, 11:03
CString probably has a direct cast to const char *, whereas QString has not, because it stores its data in Unicode (16 bit values) so a direct cast is neither possible nor desirable. The current situation forces you to think if casting is something you really want to do.

Windsoarer
26th June 2009, 06:40
Thanks,

Here is another extension to the question : using the QTextStream >> method to read the values of x,y,z, is there a way to check how many values have been read correctly ?



QTextStream line;
QString strong = in.readLine();
line.setString(&strong);
line >> x >> y >>z;


For instance with the toDouble(&bOK) function, the value of bOK gives the information, and sscanf returns the number of values read correctly

Is there anything similar for QTextStream ?

wysota
26th June 2009, 08:41
No, QTextStream assumes you know what you are reading and that all values are correct. These that are not, will take their neutral values (like "0" for integers). If you are interested in manual checks, read to string first and them use proper "to*" function to do the conversion.