在不同平台上开发C/C++程序时,为了避免源码文件乱码,得采用UTF-8编码来存储源码文件。但是很多编译器对UTF-8源码文件兼容性不佳,于是我做了一些测试,分析了最佳保存方案。
一、测试程序
为了测试编译器对UTF-8源码文件兼容性,我编写了这样的一个测试程序——
//#if _MSC_VER >= 1600 // VC2010 //#pragma execution_character_set("utf-8") //#endif #include #include #include #include char* psa = "\u4e00字A"; wchar_t* pdw = L"\u4e00字W"; int main(int argc, char* argv[]) { char* pa; wchar_t* pw; setlocale(LC_ALL, ""); // 使用系统当前代码页. // char printf("len