大佬教程收集整理的这篇文章主要介绍了字符串和文件读写相关的几个问题:,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
字符串和文件读写相关的几个问题:@H_404_1@
1. 在UNICODE工程中使用多字节可以使用CSringA@H_404_1@
2. 调用VC6.0的DLL,传入的CString一定要赋初值,不然析构会遇到问题,最好用char*代替
原因: VC6.0中CString使用引用计数
1) 每个CString都有自己的串头(内含引用计数,数据长度,已分配内存长度),紧接着后面是真正的数据。
因为是基于引用计数,所以相同的多个CString可以共享同一份数据@H_404_1@
2)每个未初始化CString都会指向同一固定的全局数据,内部引用计数、数据长度、已分配内存长度、内容分别为-1,0,0,0@H_404_1@
3)字符串析构时会检测是否已经分配内存,是否其他没有人用(引用计数小于0),都满足后才会最终释放内存@H_404_1@
如果这个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中,能防止中文乱码问题@H_404_1@
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();
}@H_404_1@
6. 使用Cppsqlite3的时候,插入数据库的中文乱码,这是由于sqlite数据库使用的是UTF-8编码方式,而传入的字符串是ASCII编码或Unicode编码
详细出处参考:http://www.code.net/article/35778.htm
将ASCII编码或Unicode编码转为UTF-8再操作数据库
下面是参考code:@H_404_1@
//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; }
以上是大佬教程为你收集整理的字符串和文件读写相关的几个问题:全部内容,希望文章能够帮你解决字符串和文件读写相关的几个问题:所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。