Reading double values with sscanf in QT
-
wrote on 22 Nov 2010, 10:14 last edited by
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
-
wrote on 22 Nov 2010, 10:26 last edited by
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.
-
wrote on 22 Nov 2010, 12:19 last edited by
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 -
wrote on 22 Nov 2010, 13:10 last edited by
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.
-
wrote on 22 Nov 2010, 14:44 last edited by
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.
-
wrote on 22 Nov 2010, 15:26 last edited by
Hi,
perfect. this is exactly the method I searched for.
with this everything works fine.Thank you very much.
1/6