Results 1 to 8 of 8

Thread: Alternative to sscanf ??

  1. #1
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Alternative to sscanf ??

    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 :

    Qt Code:
    1. CString strong;
    2. double x,y;
    3. bool bRead = XFile.ReadString(strong);
    4. int res = sscanf(strong, "%lf%lf", &x,&y);
    To copy to clipboard, switch view to plain text mode 

    How should it be done with Qt ???

    Thanks

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Alternative to sscanf ??

    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

  3. #3
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to sscanf ??

    Eventually, this is the way I did it

    Qt Code:
    1. QByteArray textline;
    2. const char *text;
    3. textline = strong.toAscii();
    4. text = textline.constData();
    5. res = sscanf(text, "%lf %lf %lf", &x, &y, &z);
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alternative to sscanf ??

    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
    Qt Code:
    1. QFile f("filename goes here");
    2. f.open();
    3. QTextStream str(&f);
    4. double x,y,z;
    5. str >> x >> y >> z;
    To copy to clipboard, switch view to plain text mode 
    Five lines including declaration and initialization of all variables which you happily ommited in "one line of MFC code".

  5. #5
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to sscanf ??

    OK, forgive my ignorance.

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alternative to sscanf ??

    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.

  7. #7
    Join Date
    Dec 2008
    Location
    Paris, France
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Alternative to sscanf ??

    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 ?

    Qt Code:
    1. QString strong = in.readLine();
    2. line.setString(&strong);
    3. line >> x >> y >>z;
    To copy to clipboard, switch view to plain text mode 

    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 ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Alternative to sscanf ??

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QKeyEvent::ascii alternative in QT4
    By psadhukhan in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2008, 12:32
  2. alternative to qtextedit
    By Rooster in forum Newbie
    Replies: 4
    Last Post: 7th August 2008, 02:36
  3. opengl alternative to setMask() ?
    By JeanC in forum Qt Programming
    Replies: 0
    Last Post: 3rd June 2008, 15:01
  4. alternative to QMessageBox
    By incapacitant in forum Newbie
    Replies: 20
    Last Post: 24th May 2006, 14:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.