ofstream创建失败问题

用 ifstream 或 ofstream 打开带有中文路径的文件会失败。

主要问题在于系统语言环境,解决办法如下:

std::locale loc = std::locale::global(std::locale(""));//设置全局locale为本地环境
std::ifstream _in("D:\\Program Files\\中文字符\\text.db");
 
int error = 0;
if (!_in.is_open())
{
	error = ::GetLastError();
	return false;
}
std::string value;
while(std::getline(_in, value))
{
	//code......
}
std::locale::global(loc);//恢复全局locale

 

posted @ 2022-07-23 16:39  香菇0_0  阅读(324)  评论(0)    收藏  举报