Sscanf() problem



  • Hi everyone!
    I need use the std::scanf() function with a QString object.

    Anyone can help me?

    I try to do this, but id doesn't works...
    @ QString Qinput = ui->lineEdit->text();
    std::string input = Qinput.toStdString();
    double a, b;
    sscanf(input, "(%f, %f)", &a, &b);
    point P(a, b);
    std::string result = P.eqLineThrough1Point();
    this->setResult(result);@



  • This is the error...

    cannot convert 'std::string*' to 'const char*' for argument '1' to 'int sscanf(const char*, const char*, ...)'

    Anyone know how to solve this problem?


  • Moderators

    Why do you need to use sscanf at all? Are you just trying to pull two floats out of a string?



  • Yes... Are there other function?


  • Moderators

    There are a number of options, all of which operate on QStrings.

    You can use a QRegExp to extract the two floats into separate QStrings, then use QString::toDouble() to process their values. There's actually an example similar to what you want in the "QRegExp::indexIn()":/doc/qt-4.8/qregexp.html#indexIn documentation.

    You also probably want to use a QValidator on your QLineEdit to make sure that the string is in the right format to begin with, though.

    (Incidentally, as a side note, in your original code, you're confusing a std::string with a const char*. They are not the same thing at all.)



  • i try to use the indexIn, but i find more simple sscanf() ;)

    I know that string and char* is not the same thing, and i want to know if there some method to convert a string in const char*, beacuse the sscanf function need a const char* in input.


  • Moderators

    bq. i try to use the indexIn, but i find more simple sscanf() ;)

    To go the simple route is to deprive yourself of skill. If you take the time once to learn to do it a proper way then not only will you have bettered yourself, but you will also have written more robust code and will have the knowledge to apply it in the future. In C++, char* strings are very far from the best way to do things. You have to worry about all kinds of low-level things, buffer overflows, and all sorts of C-type things. Why worry about that when QString handles it so much more elegantly?

    However, if you insist on doing it that way, look at "this":/forums/viewthread/4732 thread.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.