Fstream and Chinese path

Source: Internet
Author: User

Recently, When I encountered a problem that fstream could not open the Chinese path in Visual Studio 2005, I collected some solutions online. The following is an excerpt:

Fstream and Chinese path

Sometimes, opening a file with a Chinese path using ifstream or ofstream fails.

Solution:
1. Use C language functions to set the Chinese Runtime Environment
Setlocale (lc_all, "Chinese-simplified ");

2. Use STL functions to set the environment for the System Language
STD: locale: Global (STD: locale (""));

Of course, select 2!

Another reprinted in detail on the problem of discussion: http://haoren.blogbus.com/logs/2278697.html

In a program written two days ago, it was found that there was a problem with the Chinese path scan, and the CRC value and file size of the file with the Chinese path could not be obtained. Because both of the results are obtained through fstream, we tracked and found that fstream in MySQL 2005 has a bug in Chinese path processing.

And
Locate the problem after tracing. In the original 2005, the path support for STD: wfstream (this is something MS has done by itself, and wfstream is not specified in the C ++ Standard)
Wchar_t, the paths of all underlying functions are converted to wchar_t for operation. Previously, the wfstream of vc2003 does not support wchar_t as the path,
The STL writer does not use the standard Ms API: multibytetowidechar and widechartomultibyte for wchar_t and
The conversion between char is implemented using the wcstombos of the c library. Wcstombos is not a function like multibytetowidechar.
The parameter specifies the code page settings, which only accepts the locale settings of the current full sentence. When the global locale is not set as the code page of the text to be converted by wcstombos, a bug occurs.
.

The default locale is set to ansi c, and the conversion from Chinese DBCS to Unicode can be solved by setting the code page to ". 936. Therefore, to solve this bug, you can:

Setlocale (lc_ctype, ". 936 ");

Set global locale. However
This
The global locale changes. Wcstombos is a function in the Standard C library and is not a library function in STL. Therefore, it cannot be affected by the locale class alone. Set
It is possible that STD: cout won't be able to output the Chinese name correctly, and the impact may seem a little less than worth the candle. Therefore, this bug should not be solved in this way. Or wait for the ms sp patch to come out.
Check whether the problem has been solved. Before that, you can consider replacing fstream with fopen of library C.

Finally: As mentioned above, the bottom layer of fstream uses wchar_t to process the path. If we convert the path to wchar_t in advance, it is also possible, but this will also cause some problems: for example, the compiler before vc2005 cannot be compiled.

Another reprinted on the above modified caused by cout can not output Chinese solutions: http://elanso.com/ArticleModule/HaHGMbTDPKHaSYHQSsSEM6Ii.html

A few days ago I posted this article "using fstream to read and write binary files ".
A friend pointed out the bug that fstream of vs2005 does not support Chinese paths. I think it is probably because vs2005 attaches more importance to the global support for strings, so we are encouraged to use Unicode-encoded strings. We may be negligent in support for MBCS and the like.

I searched for the solution to this problem and wrote the Demo code according to the following documents.

  • Fstream and Chinese path
    C ++ blog
  • About Unicode settings in Visual Studio 2005, it really puzzled me a lot
    MS Forum
  • Msdn

I have summarized the above content and summarized the three methods to better solve everyone's troubles, including the possibility that cout cannot be used.

/*************************************** * *************************** <Br/> created: 2008/05/10 <br/> created: <br/> filename: K:/SJ/fstreamtest/main. CPP <br/> file path: K:/SJ/fstreamtest <br/> file base: Main <br/> File Ext: CPP <br/> author: gohan <br/> *********************************** ********************************/<br/> # include <br/> # include <br/> using namespace STD; <br/> int main () <br/> {<br/> /****************************** **************************************** **/<br/>/* method 1, use the _ text () macro to specify the String constant as the tchar * type */<br/>/* if it is me, this type is preferred */<br/> /****************************** **************************************** **/<br/> fstream file; <br/> file. open (_ text ("C: // test cmd.txt"); <br/> cout <PRE> <br/> file. close (); </P> <p> /******************************** **************************************** /<br/>/* method 2, use the static method of the locale class in STL to specify the global locale */<br/>/* after using this method, cout may not be able to output Chinese characters normally, very strange */<br/>/* I found a method to barely solve the problem: do not use cout or wcout to output Chinese characters before setting the restored area */<br/>/* Otherwise, the result is that the cout wcout cannot be used to output Chinese characters after setting the restored area */<br/>/ **************************************** * ******************************/<br/> locale:: Global (locale (""); // set the global region as the default region of the Operating System <br/> file. open ("C: // test cmd2.txt"); // the file can be opened successfully <br/> locale: Global (locale ("C ")); // restore the global region Settings <br/> cout <PRE> <br/> file. close (); </P> <p> /******************************** **************************************** /<br/>/* method 3, use the C function setlocale, the solution to the problem that cannot output Chinese characters using cout is the same as above */<br/> /*********************** **************************************** * ********/<br/> setlocale (lc_all, "Chinese-simplified"); // sets the Chinese environment <br/> file. open ("C: // test ipv3.txt"); // the file can be opened successfully <br/> setlocale (lc_all, "C "); // restore <br/> cout <PRE> <br/> file. close (); <br/>}

In addition, the first method, if it is not a static string as a path, remember to pass in a tchar * type string as a path, it should be OK.

 

Reprinted from: http://jackinelee.blog.hexun.com/20954695_d.html

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.