Locale problem in VC ++ STL (2)

Source: Internet
Author: User

Published in: http://www.hellocpp.net/Articles/Article/722.aspx

 

In fact, for me, this articleArticleI have encountered some problems. Today is just a whim. Let's take a closer look.

Bug Description: create a user name with a Chinese/Japanese name on the Chinese/Japanese system. An error occurred while creating the file in the user's directory.
After checking, it is found that as long as the file name contains Chinese or Japanese characters and other non-English characters, fstream will fail to create the file. I used vc2005 for the first time.
The solution was as follows:
1. InProgramCall setlocale (lc_all, "") at startup ,"");
2. Use fopen instead of fstream.
3. When fstream is used, wchar_t * is used as the parameter.

So why is this problem? The reason is that the default locale in CRT is "C" under VC ++ 2005/2008, not the default locale in the system, and, the new STL will now convert your char * file name parameter to wchar_t *. This action will use the locale of CRT, therefore, if the file name you pass to VC ++ contains Chinese characters and is converted using locale of C, garbled characters will inevitably occur. The above method can be easily explained.
1. setlocale (lc_all, "") enables the CRT to use the default locale. If you use Windows XP/Vista/win7, this should be the location in the regional language options and the location of non-Unicode programs. Then the natural conversion will be correct.
2. Even if fopen is converted internally through the createfile of Win32 API, the locale of the system will be irrelevant to the CRT.
3. You have already finished it. Of course, there is no problem.

To sum up. In fact, I personally think method 3 is king. Unicode is the perfect solution for all your things.

Of course this is not the case.
In some locale, There are some minor differences. For example, in a German system, decimal places are represented by 0, 1, rather than 0.1. if your configuration file is 0.1. After you call setlocale (lc_all, ""), in the German system, what you read in 0.1 using sscanf will be an incorrect value: 0.0f. Is it dizzy? Well, maybe the problem is not just that. Generally, locale contains many options, such as lc_ctype and lc_numeric lc_currency. Lc_numeric affects the expression of this number.

Therefore, if you only want C ++ STL to open a non-English character file name, you only need setlocale (lc_ctype.

 

At present, I will only summarize it here. I will add some other findings later. If there are any mistakes, you are welcome to raise them.

 

Note: cout cannot be output after setlocale is used, because I have never used cout output (I usually use printf to print to the console + outputdebugstring to output to the log mode of the debugging window ). So this article does not mention. If you care about this, you can set locale before opening the file. After opening the file, the default locale is set:
Locale lc = setlocale (lc_all ,"");

Fstream F (".....");

Setlocale (lc_all, LC );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.