std::locale::global 的问题

相信大家都碰到过VS2005和VS2008中使用std::ofstream打开包含中文的文件目录的问题吧。

解决方法是网上一致传诵的:
std::locale::global(std::locale("")); // 解决std::ofstream.open()中文路径文件名问题

global函数应该返回前一个locale对象,但是看了它的源码,返回的是 _Oldglobal,而这个变量自从定义以后就没有被“加工”过,怎么可以实现功能呢?


以下代码来自 Microsoft Visual Studio 9.0\VC\crt\src\locale.cpp
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#if !STDCPP_IMPLIB || !defined(_M_CEE_PURE)
 
_MRTIMP2 locale __cdecl locale::global( const  locale& loc)
     {    // change global locale
     locale _Oldglobal;
     _BEGIN_LOCK(_LOCK_LOCALE)
         locale::_Locimp *_Ptr = _Getgloballocale();
 
         if  (_Ptr != loc._Ptr)
             {    // set new global locale
             _DELETE_CRT(_Ptr->_Decref());
             _Setgloballocale(_Ptr = loc._Ptr);
             _Ptr->_Incref();
             category _Cmask = _Ptr->_Catmask & all;
             if  (_Cmask == all)
                 setlocale (LC_ALL, _Ptr->_Name.c_str());
             else
                 for  ( int  catindex = 0; catindex <= _X_MAX; ++catindex)
                     if  ((_CATMASK(catindex) & _Cmask) != 0)
                         setlocale (_CAT_TO_LC(catindex), _Ptr->_Name.c_str());
             }
         return  (_Oldglobal);
     _END_LOCK()
     }
 
#endif // STDCPP_IMPLIB || !defined(_M_CEE_PURE)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值