split/char-wchar/文件操作

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]='\0';
 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]='\0';
 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\n", pszBuf );
	printf("hello");
	delete pszBuf;
	getchar();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"/pkg/qct/software/llvm/release/arm/14.0.0/bin/clang" -g -Os -fshort-wchar -fno-strict-aliasing -Wall -Werror -Wno-array-bounds -c -include AutoGen.h -mlittle-endian -fno-short-enums -save-temps -fverbose-asm -funsigned-char -ffunction-sections -fdata-sections -fno-builtin -Wno-address -fno-asynchronous-unwind-tables -target aarch64-linux-gnu -fcolor-diagnostics -fdiagnostics-format=vi -Wno-parentheses-equality -Wno-tautological-compare -Wno-tautological-constant-out-of-range-compare -Wno-empty-body -Wno-unknown-warning-option -Wno-unused-function -Wno-bitwise-op-parentheses -mcmodel=small -ffixed-x18 -mstrict-align -fstack-protector -Wno-nonportable-include-path -Wno-misleading-indentation -fno-common -mtune=cortex-a53 -I/home/chen-docker/bin/boot/boot_images/BuildLogs/QcomPkg/SocPkg/LeMans/AU/Include -include /home/chen-docker/bin/boot/boot_images/boot/QcomPkg/Include/Library/DebugLib.h -DQCOM_EDK2_PATCH -DDISABLE_DEP -DENABLE_XN -DENABLE_ASLR -DENABLE_DEP_64 -DENABLE_EXEC_CODE_READY_TO_BOOT -DENABLE_AUTO_PLAT -DMAX_DDR_REGIONS=6 -mstrict-align -mcpu=cortex-a53 -DPRODMODE -c -o /home/chen-docker/bin/boot/boot_images/Build/LeMansAU/Core/RELEASE_CLANG140LINUX/AARCH64/MdeModulePkg/Library/UefiHiiLib/UefiHiiLib/OUTPUT/./HiiLib.obj @/home/chen-docker/bin/boot/boot_images/Build/LeMansAU/Core/RELEASE_CLANG140LINUX/AARCH64/MdeModulePkg/Library/UefiHiiLib/UefiHiiLib/OUTPUT/inc.lst /home/chen-docker/bin/boot/boot_images/edk2/MdeModulePkg/Library/UefiHiiLib/HiiLib.c /pkg/qct/software/llvm/release/arm/14.0.0/bin/clang: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory GNUmakefile:373: recipe for target '/home/chen-docker/bin/boot/boot_images/Build/LeMansAU/Core/RELEASE_CLANG140LINUX/AARCH64/MdeModulePkg/Library/UefiHiiLib/UefiHiiLib/OUTPUT/HiiLib.obj' failed Building ... /home/chen-docker/bin/boot/boot_images/edk2/MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf [AARCH64] make: *** [/home/chen-docker/bin/boot/boot_images/Build/LeMansAU/Core/RELEASE_CLANG140LINUX/AARCH64/MdeModulePkg/Library/UefiHiiLib/UefiHiiLib/OUTPUT/HiiLib.obj] Error 127 make: Nothing to be done for 'tbuild'. build.py... : error 7000: Failed to execute command make tbuild [/home/chen-docker/bin/boot/boot_images/Build/LeMansAU/Core/RELEASE_CLANG140LINUX/AARCH64/MdeModulePkg/Library/UefiHiiLib/UefiHiiLib]错误在哪里?
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值