首先发布一段测试代码
#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有关系吗?
问题还未研究完,公开博客,希望大家指点与一起探讨。
本文深入探讨了在Windows和Linux环境下,不同文件编码(如GB2312和UTF-8)对C++代码的影响,以及如何通过Boost库检测文件编码。通过具体测试案例,分析了gcc编译器在处理不同编码格式的源代码时的行为。

4650

被折叠的 条评论
为什么被折叠?



