1 wstring->string
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
std::wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
2 文件读写
读:
FILE* fp;
int Flen = 0;
char* p;
p = NULL;
fp = fopen("c:\\rcv1.txt", "rb");
fseek(fp, 0, SEEK_END);
Flen = ftell(fp);
p= (char*)malloc( sizeof(char) * (Flen+1) ); //是这么申请
if(p == NULL)//申请后判定是否申请成功
{
return;
}
fflush(pf);
fseek(fp, 0, SEEK_SET); //首先移动到文件开头再读取
fread(p, 1,Flen, fp);//读取大小无限制
free(p);
写:
FILE* fp;
int Flen = 0;
char* p;
p = NULL;
fp = fopen("c:\\rcv1.txt", "rb");
fseek(fp, 0, SEEK_END);
Flen = ftell(fp);
p= (char*)malloc( sizeof(char) * (Flen+1) ); //是这么申请
if(p == NULL)//申请后判定是否申请成功
{
return;
}
fseek(fp, 0, SEEK_SET); //首先移动到文件开头再读取
fwrite(“123”, sizeof(“123”),1, fp);//读取大小无限制
fflush(pf);
free(p);
3 char->wchar_t
char *chr=new char[wo.GetLength()] ;
可以先memset()
WideCharToMultiByte(CP_ACP,0,wo.GetBuffer(),-1,chr,wo.GetLength(),NULL,NULL);
chr为所需值
string str=chr;