字符串和文件读写相关的几个问题:
1. 在UNICODE工程中使用多字节可以使用CSringA
2. 调用VC6.0的DLL,传入的CString一定要赋初值,不然析构会遇到问题,最好用char*代替@H_403_5@ 原因: VC6.0中CString使用引用计数@H_403_5@ 1) 每个CString都有自己的串头(内含引用计数,数据长度,已分配内存长度),紧接着后面是真正的数据。@H_403_5@ 因为是基于引用计数,所以相同的多个CString可以共享同一份数据
2)每个未初始化CString都会指向同一固定的全局数据,内部引用计数、数据长度、已分配内存长度、内容分别为-1,0,0,0
3)字符串析构时会检测是否已经分配内存,是否其他没有人用(引用计数小于0),都满足后才会最终释放内存
如果这个CString是跨模块传递过来的,比如你DLL里有个导出函数void SetValue(CString strValue), @H_403_5@ 然后你外部Exe传递一个未出始化的字符串@H_403_5@ CString str; @H_403_5@ SetValue(str); @H_403_5@ 这时就会Crash。@H_403_5@ 根本原因是因为传入的字符串是在Exe里构造,但是在DLL里析构,Exe里的未初始化str指向的是Exe模块自己的全局初始值Exe!_atltmpDatanil,@H_403_5@ 而DLL内CString的全局初始值是Dll自己的Dll!_atltmpDatanil,两者比较当然不相等,@H_403_5@ 而后面的if (InterlockedDecrement(&GetData()->nRefs) <= 0)又会把引用计数从-1改成-2, @H_403_5@ 接下来就会试图delete这块不是new出来的全局内存,当然会Crash了。@H_403_5@ @H_403_5@ 3. 在UNICODE工程中必需使用char*的时候,怎么将(UNICODE)CString转换为char*呢?@H_403_5@ @H_403_5@ 1. 方法1 使用宏 :(注意,此方法不能在循环体中使用,会导致堆栈溢出)@H_403_5@ USES_CONVERSION ;@H_403_5@ pDB->execDML(T2CA(sql));@H_403_5@ @H_403_5@ 2. 方法2,使用WideCharToMultiByte,什么场合都可靠@H_403_5@ @H_403_5@ CString sql(_T(""));@H_403_5@ CString strColumName = *itList;@H_403_5@ if (!strColumName.IsEmpty())@H_403_5@ {@H_403_5@ sql.Format(_T("ALTER TABLE MES ADD %s nvarchar(128) ;"),strColumName);@H_403_5@ }@H_403_5@ int iLenOld = sql.GetLength();@H_403_5@ int lenNew = WideCharToMultiByte(CP_ACP,sql,sql.GetLength(),NULL,NULL);@H_403_5@ char * psql = new char[lenNew+1]; @H_403_5@ psql[lenNew] = '\0' ;@H_403_5@ WideCharToMultiByte(CP_ACP,sql.GetLength() + 1,psql,lenNew + 1,NULL);@H_403_5@ pDB->execDML(psql); @H_403_5@ delete psql ; //注意内存泄露@H_403_5@ @H_403_5@ 4. char*要转换为(UNICODE)CString时候可用强制转换,能运行但安全性有待考虑@H_403_5@ char* pInfo = "好罗窝得";@H_403_5@ CStringA strInfo("好罗窝得");@H_403_5@ AfxMessageBox((CString)pInfo);@H_403_5@ AfxMessageBox((CString)strInfo);@H_403_5@ @H_403_5@ 5. 读文件时,最好将文件先读到char*中,(char*虽麻烦,但却是最可靠的),再存到CString中,能防止中文乱码问题
try @H_403_5@ { @H_403_5@ CFile file(_T("E:\\VC_CODE\\TestSourceFile\\TestFile\\MESLIB\\MES_02280821.TXT"),CFile::modeRead); @H_403_5@ char* buf = NULL; DWORD dwLen = (DWORD)file.GetLength(); @H_403_5@ buf = new char[dwLen + 1]; @H_403_5@ memset(buf,dwLen + 1); @H_403_5@ file.Read(buf,dwLen); @H_403_5@ file.Close(); @H_403_5@ CString str(buf); //再在CString中去处理 @H_403_5@ delete[] buf; @H_403_5@ buf = NULL; @H_403_5@ } @H_403_5@ catch (CException* e) @H_403_5@ { @H_403_5@ e->ReportError(); @H_403_5@ e->Delete(); @H_403_5@ }
6. 使用Cppsqlite3的时候,插入数据库的中文乱码,这是由于sqlite数据库使用的是UTF-8编码方式,而传入的字符串是ASCII编码或Unicode编码@H_403_5@ 详细出处参考:http://www.jb51.net/article/35778.htm@H_403_5@ 将ASCII编码或Unicode编码转为UTF-8再操作数据库@H_403_5@ 下面是参考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; }