如何删除同一程序的GUI和命令行界面之间的区域有什么区别? [英] How do I remove the difference in locale between gui and commandline interfaces of same program?

查看:201
本文介绍了如何删除同一程序的GUI和命令行界面之间的区域有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序保存的设置文件为包含类型长双的浮点数文本。设置文件只能通过GUI(GTK2)保存,同时文件也可以通过命令行和而不会弹出界面加载。

不幸的是,用户报告中的文件,他有救了,由于他的区域设置,数字的格式用逗号和程序拒绝加载它们在命令行(发光错误消息),但似乎当加载它们图形用户界面是开放的。

我已经提出以下问题:<一href=\"http://stackoverflow.com/questions/2168597/mpfr-printf-decimal-places-locales-file-i-o-problem\">MPFR, printf的,小数,区域设置,文件I / O问题使用的setlocale(LC_ALL,C)我放在年初这说明。由于这没有工作,我把它叫 gtk_init_check 之后,但再次,它并没有区别。 (修改它确实有所作为后,我安装了几个语言环境。)

我希望程序总是使用相同的区域设置(或非本地化的区域设置 - C)对这些数据文件可以节省,但不想把事情弄糟这样做的GUI。

如何?


解决方案

在存储使用的语言环境在您的负载顶部/保存code:

 的char * LOC =的setlocale(LC_NUMERIC,NULL);

然后在每一个你写数值到您的文件呼叫实例:

 的setlocale(LC_NUMERIC,C);
/ * code这里读/写数值:
fprintf中(FD,纵横%0.20lf \\ n,img-&GT;方面);
* /
的setlocale(LC_NUMERIC,LOC);

应该做的伎俩。

The program saves a settings file as text which contains floating point numbers of type long double. The settings file can only be saved via the GUI (GTK2), while files can also be loaded via the command line and without bringing up the GUI.

Unfortunately, a user reports that in the files he has saved, due to his locale setting, the numbers are formatted with commas and the program refuses to load them (emitting error messages) from the command line but seems to load them when the GUI is open.

I have already asked the following question: MPFR, printf, decimal places, locales, file i/o problem which suggested using setlocale(LC_ALL, "C") which I placed at the beginning of main. As this did not work, I placed it after calling gtk_init_check but again, it made no difference. (EDIT It did make a difference after I installed a few locales.)

I want the program to always use the same locale setting (or non-localized locale setting - "C") for these data files it saves, but don't want to mess up the GUI by doing so.

How?

解决方案

Store the locale in use at the top of your load/save code:

char* loc = setlocale(LC_NUMERIC, NULL);

Then for every instance where you write numeric values to your file call:

setlocale(LC_NUMERIC, "C");
/* code here for read/write of numeric values:
fprintf(fd, "aspect %0.20lf\n", img->aspect);
*/
setlocale(LC_NUMERIC, loc);

Should do the trick.

这篇关于如何删除同一程序的GUI和命令行界面之间的区域有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆