Re: Problems with "atof" from <cstring> library

On Tue, 2006-04-11 at 19:50 +0200, W�old Plaum wrote:
> Hello,
> I've got a strange problem with gtkmm. In the following there are two
> little programms which are mainly the same, but just changed the
> position of "Gtk::Main kit(argc, argv);" The results are different.
> The first version does it right. The second not. Why?

i suspect that GTK sets the locale to something reflecting your location
in an area where the decimal separator is a comma not a dot/period.

if you use C library functions to convert between scalar values and
strings you must be sure to take control of the locale setting if you
want it to work predictably. Here is the C++ object i use for this:

	struct LocaleGuard {
	    LocaleGuard (const char*);
	    ~LocaleGuard ();
	    const char* old;

LocaleGuard::LocaleGuard (const char* str)
	old = strdup (setlocale (LC_NUMERIC, NULL));
	if (strcmp (old, str)) {
		setlocale (LC_NUMERIC, str);

LocaleGuard::~LocaleGuard ()
	setlocale (LC_NUMERIC, old);
	free ((char*)old);

i declare an object of that type within the scope of anything where i
need to know that locale is not intended to affect the operation of C

	LocaleGuard lg ("POSIX");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]