std::locale::global 的问题

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 12:38   50   0
相信大家都碰到过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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP