changing locals for all io on a c++ code

802 Views Asked by At

I am using e57 library (http://www.libe57.org/) and I have a e57 data set that I can read them correctly, when the decimal point in windows is set to '.', but on a system that decimal point is ',', it doesn't work properly (for example on windows with language set to Spain or Norway and several other European language).

To test the correctness my code, I am using demoRead application which is shipped by this library.

I can see that when the locals are set so decimal point is '.', application can read data for a point as :

 x=0.24965 y=-0.1595 z=-0.29536

but if I change the decimal point in windows setting to ',' Then application read these values:

 x=24965 y=-15950 z=-29536

As I don't want to look at the bug in this big library, I am wondering if there is a way that I can change the locals for my application in code?

I already tried to do this using this code:

 setlocale(LC_ALL,"en-US");

without any success. Is there any way that I can change the decimal point char in code that effect all IOs even they are deep inside a library?

2

There are 2 best solutions below

1
On

You can set LC_NUMERIC for the decimal point. For monetary-formatting you can set LC_MONETARY.

LC_NUMERIC is only for the "printf family" and localeconv

http://msdn.microsoft.com/en-us/library/x99tb11d.aspx

2
On

libE57 is a C++ library. The relevant standard library component should hence be <locale> and not <clocale>.

setlocale() sets the locale and affects many functions of the standard c library, such as printf(), scanf() and c-string functions. But it has no direct effect on the C++ library.

The c++ locale affects the c++ standard library, for example streams such as standard streams, file streams, or string streams. By default the locale is set to the default "C" locale. You can change this to the windows locale with:

locale::global(locale(""));  // sets locale according to OS environment 

And you can set it back to the default on (with "." as decimal separator):

locale::global(locale("C"));  // set locale to default locale

Such a change does however affect only new streams created. Already opened streams, such as for example cin and cout, will keep the locale that was set when they were opened. You can of course change :

cout.imbue (myloc);  // change locale use by one speciifc stream 
                     //myloc is a locale that was created using default