1

我编写了一个 C++ 函数调用的配置文件。由于操作系统中设置的某些操作系统或语言以不同方式读取浮点数,例如 ( 4.5& 4,5)

如何在运行时检查以查看正在运行的操作系统,以便我知道是否应该使用逗号或小数点读取浮点数。

有任何想法吗?

(我的代码在 Windows 和 linux(跨平台)上相同,但是当我在 linux 上读取包含浮点数的配置文件时,它没有正确读取小数点?)

前进的方向是什么?

4

3 回答 3

1

The term you're looking for is locale and the iostream member function imbue(). The example code on the imbue page gives pretty much the exact function you want.

double d;
std::locale mylocale("");
std::cin.imbue(mylocale);
std::cin << d;

should end up reading the variable in the right form. You may need to get the locale name from an environment variable or similar, depending on platform. On linux it'd come from the environment variable LANG. I'm not sure about windows.

于 2013-11-06T07:19:37.737 回答
0

You can use such function as localeconv to get struct lconv, wich contains much informations about locale settinng and setlocale to set locale information from clocale header. Here is a link to to this header: http://en.cppreference.com/w/cpp/locale

于 2013-11-06T07:19:36.673 回答
0

由于无论如何您都必须为不同的平台重新编译程序,因此使用编译时定义很容易:

#ifdef WIN32
/* Do Windows stuff */
#endif
#ifdef LINUX
/* Do Linux stuff */
#endif

然后,例如,当您在 linux 上编译时,使用g++ -DLINUX ...,或使用您的构建系统传递标志。

但我怀疑在这种情况下,如果您尝试以与平台无关的方式使用用户的语言环境读取浮点数,您可能最好查看这些std::locale内容(即使它真的很可怕 IMO)。

于 2013-11-06T07:11:16.140 回答