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. Reading double values with sscanf in QT
QtWS25 Last Chance

Reading double values with sscanf in QT

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.3k Views
  • 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.
  • B Offline
    B Offline
    Blackthorn
    wrote on last edited by
    #1

    Hi,

    I am new to this forum and this is my first post.
    I am developing a graphical user interface for my main program. The main program is written in standard C++, the GUI is written in QT. Up to now it works really fine.

    But now I have a problem with the sscanf-function of C++.

    I am reading from a text file and save each line in a string which I split with the sscanf-function. The lines contains several double values as well as integer and character values. These are saved into variables.
    In the original program this function causes no problems.

    In QT, the language settings seem to play a role for that sscanf function. I am developing on a system with german language layout. Therefore, double variables have to be written written with commas (e.g. 1,45). In english language layout these are written with dots (1.45). Unfortunately, my input files always have to contain dots (due to specification and setting of the program, this cannot be changed!).
    When I change my input file for testings and replace the dots with commas, the sscanf function in QT works perfectly.
    My bugfix up to now is searching the read string for dots and replace them by commas before splitting with the sscanf-function.
    As my main program is quite large and several persons are deleoping on it, I don't want to change the whole code. Furthermore, the program would not run on systems with english layout.

    Is there a function in QT where I can change the layout for numbers manually and how do I have to add this to my program? Can I make these setting globally for the whole program?
    I already searched the forum and google but did not find a solution for my problem.

    I hope you can help me.

    Greetings,

    Christoph

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      Hi,

      well, you can use QLocale::toDouble method (and all other methods present in QLocale). Of course, you need to setup all regional infos in QLocale before using it.

      Look for QLocale in your Qt documentation.

      T.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Blackthorn
        wrote on last edited by
        #3

        Hi,

        thank you for your reply.
        the QLocale::toDouble method would be a possibility to check the strings and double values.
        but there, I have to add QLocale::toDouble() before every sscanf-call in my program and this would not be the best solution for me, as many persons are developing on this project (which is quite big) and the same code should be used for a GUI version and console version (at least the code which is calculating the desired results).
        In the documentation of QLocale there are many methods, but I don't understand how I should use them to solve this problem?
        The best thing would be something, where I can choose in the constructor of my program that the language should be the system language but the numbers should always be in english layout. It would also be ok that everything is in english layout no matter what the system layout would be.

        Greeting,
        Christoph

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tony
          wrote on last edited by
          #4

          First of all, I think that maybe it's better for you to remove the sscanf usage. You can just read a full line, split it in a QStringList (using the QString::split method), and then read each single value. I guess you know the type in advance, according to the position. I hope that I got your point.

          Anyway, you can build QLocale in English in this way:

          QLocale locale(QLocale::English,QLocale::UnitedStates)

          and use "locale" to convert all the strings to the numbers you need. If you need a variant from UnitedStates one, just check in QLocale documentation all the variants you can use.

          T.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            See "QCoreApplication/Locale Settings":http://doc.qt.nokia.com/4.7/qcoreapplication.html#locale-settings for a discussion of that. In short: You could call

            @
            setlocale(LC_NUMERIC,"C")
            @

            Right after initialising QApplication/QCoreApplication.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Blackthorn
              wrote on last edited by
              #6

              Hi,

              perfect. this is exactly the method I searched for.
              with this everything works fine.

              Thank you very much.

              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