输出中文
locale loc( "chs" ); wcout.imbue( loc ); wcout << getexepath().getbuffer() << endl;
编码转换
#pragma once #include <windows.h> #include <atlstr.h> #include <string> typedef std::basic_string<tchar, std::char_traits<tchar>,std::allocator<tchar> > tstring; std::string t2a(cstring input) { uses_conversion; return t2a(input); } std::string t2a(tstring input) { uses_conversion; return t2a(input.c_str()); } cstring a2t(std::string input) { uses_conversion; return a2t(input.c_str()); } tstring a2t_2(std::string input) { uses_conversion; return a2t(input.c_str()); } char* unicodetoutf8(const wchar_t* unicode) { int len; len = widechartomultibyte(cp_utf8, 0, unicode, -1, null, 0, null, null); char *szutf8 = (char*)malloc(len + 1); memset(szutf8, 0, len + 1); widechartomultibyte(cp_utf8, 0, unicode, -1, szutf8, len, null, null); return szutf8; } cstring utf82wcs(const char* szu8) { //预转换,得到所需空间的大小; int wcslen = ::multibytetowidechar(cp_utf8, null, szu8, strlen(szu8), null, 0); //分配空间要给' '留个空间,multibytetowidechar不会给' '空间 wchar_t* wszstring = new wchar_t[wcslen + 1]; //转换 ::multibytetowidechar(cp_utf8, null, szu8, strlen(szu8), wszstring, wcslen); //最后加上' ' wszstring[wcslen] = ' '; cstring unicodestring(wszstring); delete[] wszstring; wszstring = null; return unicodestring; }
读取文件
#pragma once #include <fstream> #include <sstream> #include <string> std::string getfilecontents(std::string filename) { std::ifstream ifs(filename.c_str()); if (ifs.good()) { std::stringstream ss; ss << ifs.rdbuf(); return ss.str(); } return ""; } std::string getfilecontents2(std::string filename) { std::ifstream ifs(filename.c_str()); if (ifs.good()) { std::string strbuffer((std::istreambuf_iterator<char>(ifs)),std::istreambuf_iterator<char>()); return strbuffer; } return ""; }
获取文件路径
#pragma once #include <atlstr.h> // 获取当前可执行文件的路径 cstring getexepath() { cstring strcurpath; getmodulefilename(null, strcurpath.getbuffer(max_path), max_path); strcurpath.releasebuffer(); return strcurpath.left(strcurpath.reversefind(_t('\'))); } // 获取当前可执行文件的文件名 cstring getexename() { cstring strcurpath; getmodulefilename(null, strcurpath.getbuffer(max_path), max_path); strcurpath.releasebuffer(); return strcurpath.mid(strcurpath.reversefind(_t('\'))+1); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/605123.html