472,373 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Mixing setlocale() and c++ locales

I was working with a co-worker the other day to work through the
process of formatting numeric values by imbueing C++ iostreams with
locales. His program's initialisation code had a call to
setlocale("",LC_ALL) which I believe sets up locale information for the
C run time library but is not supposed to effect the C++ run time.
What we found, however, was that, with this function call in place, the
C++ iostreams were formatting floating point values according to the US
convention (commas as a thousands separator and periods as a decimal)
even though the stream was imbued with a different locale (Brasilian
Portuguese in our case). We found that, if we removed the above
mentioned call that the iostream formatting worked aas we expected. My
question is whether this interaction is a bug or is it rather an
unfortunate but planned side effect that comes from mixing the "C"
and C++ locales models?

Regards,

Jon Trauntvein

Nov 17 '05 #1
3 2695
J Trauntvein wrote:
I was working with a co-worker the other day to work through the
process of formatting numeric values by imbueing C++ iostreams with
locales. His program's initialisation code had a call to
setlocale("",LC_ALL) which I believe sets up locale information for the
C run time library but is not supposed to effect the C++ run time.
What we found, however, was that, with this function call in place, the
C++ iostreams were formatting floating point values according to the US
convention (commas as a thousands separator and periods as a decimal)
even though the stream was imbued with a different locale (Brasilian
Portuguese in our case). We found that, if we removed the above
mentioned call that the iostream formatting worked aas we expected. My
question is whether this interaction is a bug or is it rather an
unfortunate but planned side effect that comes from mixing the "C"
and C++ locales models?


Do you have a test case? The following prints "1,234" for me on VC7.1:

#include <iostream>
#include <locale>
#include <locale.h>

int main()
{
std::locale loc("Portuguese_Brazil");
std::cout.imbue(loc);
setlocale(LC_ALL, "");
std::cout << 1.234 << '\n';
}

Moving the setlocale call to the start makes no difference. The C and
C++ locale systems are synchronized to a degree. See
http://www.dinkumware.com/manuals/re...e2.html#locale
for some info on this, or check the C++ standard.

Tom
Nov 17 '05 #2

Tom Widmer wrote:
J Trauntvein wrote:
I was working with a co-worker the other day to work through the
process of formatting numeric values by imbueing C++ iostreams with
locales. His program's initialisation code had a call to
setlocale("",LC_ALL) which I believe sets up locale information for the C run time library but is not supposed to effect the C++ run time.
What we found, however, was that, with this function call in place, the C++ iostreams were formatting floating point values according to the US convention (commas as a thousands separator and periods as a decimal) even though the stream was imbued with a different locale (Brasilian Portuguese in our case). We found that, if we removed the above
mentioned call that the iostream formatting worked aas we expected. My question is whether this interaction is a bug or is it rather an
unfortunate but planned side effect that comes from mixing the "C" and C++ locales models?
Do you have a test case? The following prints "1,234" for me on

VC7.1:
#include <iostream>
#include <locale>
#include <locale.h>

int main()
{
std::locale loc("Portuguese_Brazil");
std::cout.imbue(loc);
setlocale(LC_ALL, "");
std::cout << 1.234 << '\n';
}

Moving the setlocale call to the start makes no difference. The C and C++ locale systems are synchronized to a degree. See
http://www.dinkumware.com/manuals/re...e2.html#locale
for some info on this, or check the C++ standard.

Tom


I just tried the following code:

#include <iostream>
#include <locale.h>
int main()
{
double const val = 123456.789;

setlocale(LC_ALL,"");
std::cout.imbue(std::locale("Portuguese_Brazil"));
std::cout << val << std::endl;
return 0;
} // main
This produces the behaviour that I described provided that the
operating system regional settings are set to Brasilian Portuguese. It
does not appear when the regional locale is set to US English.
Commenting out the setlocale() call produces the correct behaviour.

Regards,

Jon Trauntvein

Nov 17 '05 #3
J Trauntvein wrote:
I just tried the following code:

#include <iostream>
#include <locale.h>
int main()
{
double const val = 123456.789;

setlocale(LC_ALL,"");
std::cout.imbue(std::locale("Portuguese_Brazil"));
std::cout << val << std::endl;
return 0;
} // main
This produces the behaviour that I described provided that the
operating system regional settings are set to Brasilian Portuguese. It
does not appear when the regional locale is set to US English.
Commenting out the setlocale() call produces the correct behaviour.


Ok, I managed to get the same behaviour. Note that changing:
setlocale(LC_ALL,"");
to
std::locale::global(std::locale(""));
gives the same effect without using the C library.

Fiddling around with the regional settings and the code, I didn't work
out the the logic behind the way it works (or, rather, doesn't work).
I'd recommend just removing the call to setlocale if at all possible.
Further, I'd recommend posting in microsoft.public.vc.stl, where
P.J.Plauger and Pete Becker (both of Dinkumware, the standard library
supplier for VC) are likely to see it, and hopefully tell you how it
works, and how to fix your problem.

Tom

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: transam | last post by:
Hi, I use Mandrake linux 10 with German setup, Hungarian Keyboard. My Python is python 2.3.3. The following program fails: --------------------------------------------------------- import...
3
by: Ksenia Marasanova | last post by:
Hi, I have some problems with locale module. On my workstation, changing locale doesn't have effect: Python 2.3 (#1, Sep 13 2003, 00:49:11) on darwin Type "help", "copyright", "credits" or...
1
by: Mathieu Malaterre | last post by:
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...
3
by: Damien Elmes | last post by:
Hi folks, I've got a module + C extension which provides on screen display support in X, via libxosd (http://repose.cx/pyosd). I've recently had a report of trouble where a Russian user was...
3
by: Schraalhans Keukenmeester | last post by:
Hi, I run Apache 1.3.33 and PHP 5.0.4 on my Suse9.2 Linux box. If I try any of the following: setlocale (LC_TIME,'nl'); setlocale (LC_TIME,'nl_NL'); //example php.net site setlocale...
0
by: Paul Lautman | last post by:
Last night I had a web site. This morning it was returning nothing. I traced it to a call to setlocale(). It appears that something has happened that means that any call to setlocale completely...
7
by: Steven Woody | last post by:
Hi, I am in Linux writing a program using setlocale(3). But I found, only the first invocation of setlocale(3) can be success, any subsequent calling of this function will fail ( return...
5
by: yogeshmk | last post by:
I'm writing an application which is required to function with many languages and also compile on Linux & windows. Here's how I find the locale .. # include <stdio.h> # include <locale.h> int...
0
by: bestbikeever | last post by:
from manual: Warning: The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server api like IIS or Apache on Windows you may experience...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.