1>分割字符串
#pragma once
#include <string>
#include <vector>
using namespace std;
//分割字符串,之间用逗号分开
void CO_Split(const wstring &src, wstring separate_character,vector<wstring>& strs)
{
//分割字符串长度,这样就可以支持多字符串的分隔符
int separate_characterLen = separate_character.length();
int lastPosition = 0, index = -1;
while (-1 != (index = src.find(separate_character, lastPosition)))
{
strs.push_back(src.substr(lastPosition, index - lastPosition));
lastPosition = index + separate_characterLen;
}
wstring lastString = src.substr(lastPosition);
if (!lastString.empty())
strs.push_back(lastString);
}
2> wchar->char
char* WcharToChar(const WCHAR* wcr)
{
char* pchar=NULL;
int len=WideCharToMultiByte(CP_ACP,0,wcr,-1,NULL,0,NULL,NULL);
pchar=new char[len+1];
WideCharToMultiByte(CP_ACP,0,wcr,-1,pchar,len,NULL,NULL);
pchar[len]=' ';
return pchar;
}
int wcharTochar(const wchar_t* src,char* dest)
{
int size = ::WideCharToMultiByte( 936, 0, src, -1, NULL, 0, NULL, FALSE );
if (size > 0 )
{
size = ::WideCharToMultiByte( 936, 0, src, -1, dest, size, NULL, FALSE );
}
return size;
}
3> char->wchar
WCHAR* CharToWchar(const char* c)
{
WCHAR* pwchar=NULL;
int len=MultiByteToWideChar(CP_ACP,0,c,-1,NULL,0);
pwchar=new wchar_t[len+1];
MultiByteToWideChar(CP_ACP,0,c,-1,pwchar,len);
pwchar[len]=' ';
return pwchar;
}
int charTowchar(const char* src,wchar_t* dest)
{
int size = ::MultiByteToWideChar( 936, 0, src, -1, NULL, 0);
if (size > 0 )
{
size = ::MultiByteToWideChar( 936, 0, src, -1, dest, size);
}
return size;
}
//文件操作
//-------------------------------------------------------------------------------
//创建文件
bool CO_Create()
{ //创建文件
bool isCreation=true;
HANDLE hFile = CreateFileW(L"C:\save_radio_list.txt",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (INVALID_HANDLE_VALUE==hFile)
{
isCreation=false;
printf("创建文件失败!");
}
//关闭文件
CloseHandle(hFile);
return isCreation;
}
//读文件
CHAR* CO_Read()
{
HANDLE hFile = CreateFileW(L"C:\save_radio_list.txt",GENERIC_READ,0,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
//获取文件长度
DWORD fileSize = GetFileSize( hFile, NULL);
//分配空间
CHAR* pszBuf=new CHAR[fileSize + 1];
memset( pszBuf, 0, fileSize + 1 );
//设置当前读取的位置
SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
//读取数据
DWORD nRead = 0;
ReadFile( hFile, pszBuf, fileSize+1, &nRead, NULL );
CloseHandle( hFile );
return pszBuf;
//free( pszBuf );
////关闭文件
//CloseHandle( hFile );
}
//写文件
void CO_Write(CHAR* szBuff)
{
DWORD nWritten = 0;
HANDLE hFile = CreateFileW(L"C:\save_radio_list.txt",GENERIC_WRITE,0,NULL,TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile( hFile, szBuff, (DWORD)strlen(szBuff),&nWritten, NULL);
FlushFileBuffers(hFile);
CloseHandle( hFile );
}
//main
void main()
{
CHAR* pszBuf=NULL;
CHAR* str="1234567890asadf";
CO_Create();
CO_Write(str);
pszBuf=CO_Read();
printf( "result:%s
", pszBuf );
printf("hello");
delete pszBuf;
getchar();
}