Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sscanf() problem
Forum Updated to NodeBB v4.3 + New Features

Sscanf() problem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rikytres
    wrote on last edited by
    #1

    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);@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rikytres
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

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

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rikytres
          wrote on last edited by
          #4

          Yes... Are there other function?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            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.)

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rikytres
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #7

                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.

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved