views:

171

answers:

1

A user of my program has reported problems reading a settings file written by my program. I looked at the settings file in question and instead of decimal points using the period "." it uses commas ",".

I'm assuming this is to do with locales?

The file i/o is using fprintf and mpfr_out_str for file output and getline combined with atol, atof, mpfr_set_str, etc for file input.

What do I do here? Should I force my program to always use periods even if the machine's locale wants to use commas? If so, where do I start?

Edit: I've just noticed that this problem occurs when specifying the settings file to use on the command line instead of loading it via the GUI - would this indicate a problem on the OP's machine or in my code?

A: 

Do you call setlocale at all? If not, I would suggest either embedding the locale used to generate the file in the settings file or force all settings file I/O to use the C locale, via the previous suggestion of setlocale(LC_ALL, "C").

One other option is to use the locale specific formatting functions (suffixed with _l in MSVC) and create the C locale explicitly, via _create_locale(LC_ALL, "C").

MSN
Ah, sorry forgot to mention Linux. Nevertheless, `setlocale` is C89... Is forcing C locale acceptable application behaviour?
James Morris
I could apply the C locale only to numeric formatting like so `setlocale(LC_NUMERIC, "C")`.
James Morris
I have accepted this answer finally, but not without further investigation. See also: http://stackoverflow.com/questions/2171527/how-do-i-remove-the-difference-in-locale-between-gui-and-commandline-interfaces-o
James Morris
Yes, well, ideally you'd go and store this data out without using locale specific formatting (i.e., not text), but that's probably an entirely different question.
MSN