微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

字符串和文件读写相关的几个问题:

字符串和文件读写相关的几个问题:

1. 在UNICODE工程中使用多字节可以使用CSringA

2. 调用VC6.0的DLL,传入的CString一定要赋初值,不然析构会遇到问题,最好用char*代替
原因: VC6.0中CString使用引用计数
1) 每个CString都有自己的串头(内含引用计数,数据长度,已分配内存长度),紧接着后面是真正的数据。
因为是基于引用计数,所以相同的多个CString可以共享同一份数据

2)每个未初始化CString都会指向同一固定的全局数据,内部引用计数、数据长度、已分配内存长度、内容分别为-1,0,0,0

3)字符串析构时会检测是否已经分配内存,是否其他没有人用(引用计数小于0),都满足后才会最终释放内存

如果这个CString是跨模块传递过来的,比如你DLL里有个导出函数void SetValue(CString strValue),
然后你外部Exe传递一个未出始化的字符串
CString str;
SetValue(str);
这时就会Crash。
根本原因是因为传入的字符串是在Exe里构造,但是在DLL里析构,Exe里的未初始化str指向的是Exe模块自己的全局初始值Exe!_atltmpDatanil,
而DLL内CString的全局初始值是Dll自己的Dll!_atltmpDatanil,两者比较当然不相等,
而后面的if (InterlockedDecrement(&GetData()->nRefs) <= 0)又会把引用计数从-1改成-2,
接下来就会试图delete这块不是new出来的全局内存,当然会Crash了。

3. 在UNICODE工程中必需使用char*的时候,怎么将(UNICODE)CString转换为char*呢?

1. 方法1 使用宏 :(注意,此方法不能在循环体中使用,会导致堆栈溢出)
USES_CONVERSION ;
pDB->execDML(T2CA(sql));

2. 方法2,使用WideCharToMultiByte,什么场合都可靠

CString sql(_T(""));
CString strColumName = *itList;
if (!strColumName.IsEmpty())
{
sql.Format(_T("ALTER TABLE MES ADD %s nvarchar(128) ;"),strColumName);
}
int iLenOld = sql.GetLength();
int lenNew = WideCharToMultiByte(CP_ACP,sql,sql.GetLength(),NULL,NULL);
char * psql = new char[lenNew+1];
psql[lenNew] = '\0' ;
WideCharToMultiByte(CP_ACP,sql.GetLength() + 1,psql,lenNew + 1,NULL);
pDB->execDML(psql);
delete psql ; //注意内存泄露

4. char*要转换为(UNICODE)CString时候可用强制转换,能运行但安全性有待考虑
char* pInfo = "好罗窝得";
CStringA strInfo("好罗窝得");
AfxMessageBox((CString)pInfo);
AfxMessageBox((CString)strInfo);

5. 读文件时,最好将文件先读到char*中,(char*虽麻烦,但却是最可靠的),再存到CString中,能防止中文乱码问题

try
{
CFile file(_T("E:\\VC_CODE\\TestSourceFile\\TestFile\\MESLIB\\MES_02280821.TXT"),CFile::modeRead);
char* buf = NULL; DWORD dwLen = (DWORD)file.GetLength();
buf = new char[dwLen + 1];
memset(buf,dwLen + 1);
file.Read(buf,dwLen);
file.Close();
CString str(buf); //再在CString中去处理
delete[] buf;
buf = NULL;
}
catch (CException* e)
{
e->ReportError();
e->Delete();
}

6. 使用Cppsqlite3的时候,插入数据库中文乱码,这是由于sqlite数据库使用的是UTF-8编码方式,而传入的字符串是ASCII编码或Unicode编码
详细出处参考:http://www.jb51.net/article/35778.htm
将ASCII编码或Unicode编码转为UTF-8再操作数据库
下面是参考code:

//Unicode CString转UTF-8 char*
	char *Cppsqlite3DB::unicodetoUtf8(const WCHAR *zWideFilename)   
    {   
	int nByte;   
	char *zFilename;   
	nByte = WideCharToMultiByte(CP_UTF8,zWideFilename,-1,0);   
	zFilename = (char *)malloc(nByte);   
	if(zFilename == 0)   
	{   
		return 0;   
	}   
	nByte = WideCharToMultiByte(CP_UTF8,zFilename,nByte,0);   
	if( nByte == 0 )   
	{   
		free(zFilename);   
		zFilename = 0;   
	}   
	return zFilename;   
   } 

   //UTF-8转Unicode 
std::wstring Utf82Unicode(const std::string& utf8string) 
{ 
int widesize = ::MultiBytetoWideChar(CP_UTF8,utf8string.c_str(),0); 
if (widesize == ERROR_NO_UNICODE_TRANSLATION) 
{ 
throw std::exception("Invalid UTF-8 sequence."); 
} 
if (widesize == 0) 
{ 
throw std::exception("Error in conversion."); 
} 
std::vector<wchar_t> resultstring(widesize); 
int convresult = ::MultiBytetoWideChar(CP_UTF8,&resultstring[0],widesize); 
if (convresult != widesize) 
{ 
throw std::exception("La falla!"); 
} 
return std::wstring(&resultstring[0]); 
} 
//unicode 转为 ascii 
string WideByte2Acsi(wstring& wstrcode) 
{ 
int asciisize = ::WideCharToMultiByte(CP_OEMCP,wstrcode.c_str(),NULL); 
if (asciisize == ERROR_NO_UNICODE_TRANSLATION) 
{ 
throw std::exception("Invalid UTF-8 sequence."); 
} 
if (asciisize == 0) 
{ 
throw std::exception("Error in conversion."); 
} 
std::vector<char> resultstring(asciisize); 
int convresult =::WideCharToMultiByte(CP_OEMCP,asciisize,NULL); 
if (convresult != asciisize) 
{ 
throw std::exception("La falla!"); 
} 
return std::string(&resultstring[0]); 
} 
//utf-8 转 ascii 
string UTF_82ASCII(string& strUtf8Code) 
{ 
string strRet(""); 
//先把 utf8 转为 unicode 
wstring wstr = Utf82Unicode(strUtf8Code); 
//最后把 unicode 转为 ascii 
strRet = WideByte2Acsi(wstr); 
return strRet; 
} 
/////////////////////////////////////////////////////////////////////// 
//ascii 转 Unicode 
wstring Acsi2WideByte(string& strascii) 
{ 
int widesize = MultiBytetoWideChar (CP_ACP,(char*)strascii.c_str(),0); 
if (widesize == ERROR_NO_UNICODE_TRANSLATION) 
{ 
throw std::exception("Invalid UTF-8 sequence."); 
} 
if (widesize == 0) 
{ 
throw std::exception("Error in conversion."); 
} 
std::vector<wchar_t> resultstring(widesize); 
int convresult = MultiBytetoWideChar (CP_ACP,widesize); 
if (convresult != widesize) 
{ 
throw std::exception("La falla!"); 
} 
return std::wstring(&resultstring[0]); 
} 
//Unicode 转 Utf8 
std::string Unicode2Utf8(const std::wstring& widestring) 
{ 
int utf8size = ::WideCharToMultiByte(CP_UTF8,widestring.c_str(),NULL); 
if (utf8size == 0) 
{ 
throw std::exception("Error in conversion."); 
} 
std::vector<char> resultstring(utf8size); 
int convresult = ::WideCharToMultiByte(CP_UTF8,utf8size,NULL); 
if (convresult != utf8size) 
{ 
throw std::exception("La falla!"); 
} 
return std::string(&resultstring[0]); 
} 
//ascii 转 Utf8 
string ASCII2UTF_8(string& strAsciiCode) 
{ 
string strRet(""); 
//先把 ascii 转为 unicode 
wstring wstr = Acsi2WideByte(strAsciiCode); 
//最后把 unicode 转为 utf8 
strRet = Unicode2Utf8(wstr); 
return strRet; 
} 





    
		
	

原文地址:https://www.jb51.cc/sqlite/200952.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐