首先发布一段测试代码
#include <iostream>
#include <locale>
#include "boost/locale.hpp"int main()
{std::locale loc = boost::locale::generator().generate("");std::cout << std::use_facet<boost::locale::info>(loc).encoding() << std::endl;return 0;
}
1、windows系统
- 在cmd中通过运行chcp显示:活动代码页:936
- 通过vs2013创建的文件,如***.cpp,其中文件编码为GB2312;利用vs2013的“文件”->"高级保存选项"查看到的文件编码为“简体中文(GB2312) - 代码页936”
- 利用notepad++创建的文件,文件的默认编码为utf-8
- 通过上面的测试代码,运行出来的结果为utf-8
2、Linux系统
- 系统的locale为en_USS.utf8,运行上面的代码,得到的结果为utf8
- 系统的locale设置为zh_CN.gb2312,运行上面的代码,得到的结果为gb2312
- 在系统locale为en_USS.utf8的情况下,又做了如下测试:
写了如上一段测试代码,利用gcc进行编译,测试结果如下#include <unistd.h> #include <string.h> #include <string>#define INI_FILE “中文.ini”CJnSdkLog g_logFileSvr("Log", "JnFileServer");int main(int argc, char* argv[]) {char szDllName[32]; // dll名称char szDllPath[256]; // dll路径char szIniFile[256]; // INI文件memset(szDllName, 0, sizeof(szDllName));memset(szDllPath, 0, sizeof(szDllPath));memset(szIniFile, 0, sizeof(szIniFile));memset(szIniFile, 0, sizeof(szIniFile));snprintf(szIniFile, sizeof(szIniFile), "%s/%s", szDllPath, "中文.ini");std::string strConfigFile = szIniFile;// 检查文件是否存在if (FileExists(strConfigFile.c_str()) == false){return -1;}return 0; }
(1)将代码文件以gb2312的方式进行保存,调试发现中文被编码gb2312
(2) 将代码文件以utf8的方式进行保存,调试发现中文被编码utf8
以上的结果,是与gcc有关系吗?
问题还未研究完,公开博客,希望大家指点与一起探讨。