1

I crawled the web a lot and got not, what I searched for even not in SO. So I consider this a new question: How to reset the locale settings to a state when the program is starting (e.g. first after main()) ?

int
main( int argc, char **argv )
{
    // this is start output I like to have finally again
    std::cout << 24500 << " (start)\n";

    // several modifications ... [e.g. arbitrary source code that cannot be changed]
    std::setlocale( LC_ALL, "" );
    std::cout << 24500 << " (stage1)\n";
    std::locale::global( std::locale( "" ) );
    std::cout << 24500 << " (stage2)\n";
    std::cout.imbue( std::locale() );
    std::cout << 24500 << " (stage3)\n";
    std::wcerr.imbue( std::locale() );
    std::cout << 24500 << " (stage4)\n";

    // end ... here goes the code to reset to the entry-behaviour (but won't work so far)
    std::setlocale( LC_ALL, "C" );
    std::locale::global( std::locale() );
    std::cout << 24500 << " (end)\n";
    return 0;
}

Output:

24500 (start)
24500 (stage1)
24500 (stage2)
24.500 (stage3)
24.500 (stage4)
24.500 (end)

The line with the "(end)" should show the same number formatting as "(start)" ...

Does anyone know how (in a general! portable?) way ?

Jesko
  • 85
  • 11
  • 1
    Does this answer your question? [how do I set the proper initial locale for a C++ program on Windows?](https://stackoverflow.com/questions/571359/how-do-i-set-the-proper-initial-locale-for-a-c-program-on-windows) (the question says "on Windows" but the answer is portable AFAICT) – Nelfeal Nov 05 '22 at 15:10
  • Unfortunately not: gives me "24.500 (end)" ... same as before ... (on Win10 with VS2019) – Jesko Nov 05 '22 at 15:16
  • Probably because you call `std::cout.imbue` as well. – Nelfeal Nov 05 '22 at 15:20
  • Code in the section "several modifications ..." stand for "some code I cannot change in between ...". So the solution should be something which is completely independent from what is happening before ... ==> Question edited to reflect this – Jesko Nov 05 '22 at 15:23
  • Do you really need the extra stuff that `std::endl` does? `'\n'` ends a line. – Pete Becker Nov 05 '22 at 16:20
  • Here is some [my old answer for similar problem](https://stackoverflow.com/a/67819605/1387438), which explains in details how to properly setup locale to no have a problem with encoding. Solution was tested on Windows 10 and test result are included. – Marek R Nov 07 '22 at 16:37

1 Answers1

5

A few points to start with:

  • calls to std::setlocale do not affect C++ iostream functions, only C functions such as printf;
  • calls to std::locale::global only change what subsequent calls to std::locale() return (as far as I understand it), they do not directly affect iostream functions;
  • your call to std::wcerr.imbue does nothing since you don't use std::wcerr afterward.

To change how std::cout formats things, call std::cout.imbue. So this line at the end should work:

std::cout.imbue( std::locale("C") );

You can also reset the global locale, but that isn't useful unless you use std::locale() afterward (without arguments). This would be the line:

std::locale::global( std::locale("C") );

You can also do both in order (note no "C" in the imbue call):

std::locale::global( std::locale("C") );
std::cout.imbue( std::locale() );
Nelfeal
  • 12,593
  • 1
  • 20
  • 39
  • Great: So far it looks very good! Havn't checked the other locale settings to be reset, but adding the two lines before printing the line and outputting the number line shows. "24500 (end)" which is correct. Thanks a lot! – Jesko Nov 05 '22 at 15:34
  • 1
    Here is some [my old answer for similar problem](https://stackoverflow.com/a/67819605/1387438), which explains in details how to properly setup locale to no have a problem with encoding. Solution was tested on Windows 10 and test result are included. – Marek R Nov 07 '22 at 16:37