Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Mixing setlocale() and c++ locales

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
J Trauntvein - 15 Mar 2005 18:08 GMT
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
Tom Widmer - 15 Mar 2005 19:09 GMT
> I was working with a co-worker the other day to work through the
> process of formatting numeric values by imbueing C++ iostreams with
[quoted text clipped - 10 lines]
> 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/reader.aspx?b=p/&h=locale2.html#locale
for some info on this, or check the C++ standard.

Tom
J Trauntvein - 16 Mar 2005 04:02 GMT
> > I was working with a co-worker the other day to work through the
> > process of formatting numeric values by imbueing C++ iostreams with
[quoted text clipped - 7 lines]
> > 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"
[quoted text clipped - 15 lines]
>
> 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/reader.aspx?b=p/&h=locale2.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
Tom Widmer - 16 Mar 2005 19:37 GMT
 > I just tried the following code:

> #include <iostream>
> #include <locale.h>
[quoted text clipped - 13 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.