strtod / setlocale

M

Mathieu Malaterre

Hello,

I would like to have some advices on this problem I am having. In my
code I have hardcoded a string like:

const char foo[] = "11 0.438482 ";

I was then calling strtod to transform it back to double. Unfortunately
depending on the LOCALE settings, the strtod could fail.

Browsing through some previous post, it seems atof is not affected by
the LOCALE, correct ? Do you think this would be the correct approach to
solve my problem.

Is there a list of function that are affected by LOCALE and could fail
like strtod. My guess is that sscanf would fail the same way, but I
cannot think of any other function, where the dot versus comma would
change the behavior.

Thanks for any help
Mathieu
 
B

Buster

Mathieu said:
Hello,

I would like to have some advices on this problem I am having. In my
code I have hardcoded a string like:

const char foo[] = "11 0.438482 ";

I was then calling strtod to transform it back to double. Unfortunately
depending on the LOCALE settings, the strtod could fail.

Hardcode a double instead, and write it to a string. That way you'll get
the right decimal-point characters for your users' locales. That, or
write your own string_to_double function that does what you want. It
won't be hard.
Browsing through some previous post, it seems atof is not affected by
the LOCALE, correct ?

Well, I don't know. Looking it up in the docs is probably a faster way
to find out than browsing comp.lang.c++. Hold on, let's see ... in my
reference book, locale isn't mentioned under strtod or atof, and in the
UNIX manpages (Red Hat newlib C library) for strtod and atof the decimal
point character is explicitly given as '.'. You'd have to check the C
standard and/or the documentation for your standard library
implementation to be sure.
Do you think this would be the correct approach to
solve my problem.

What is your problem?
Is there a list of function that are affected by LOCALE and could fail
like strtod.

Buy yourself a good reference book, and look it up.
My guess is that sscanf would fail the same way, but I
cannot think of any other function, where the dot versus comma would
change the behavior.

A formatted read (>>) from a C++ istream into an object of built-in
numeric type is also affected by the stream's locale.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top