本人在编制代码时发现字符串中的汉字不能处理,后参考一些资料知道汉字属于宽字符,刚开始我还以为是网络传输出现了问题,后来本人经过调试发现,从liunx下能准确的得到数据(包括汉字),后确定是在WriteStringFile这个函数执行中出现了错误,而且本人经过断点调试发现在加密循环中,到处理汉字字符时,就进入了死循环,我想也许因为汉字是宽字符,必须采取一些特殊措施,如汉字字符的第一位为1,我也编写了一些代码,但是调试时还是发现,在处理汉字时进入了死循环,也许是char字符类型不识别汉字字符吧,也是本人资质驽钝!希望有高手帮忙解决!现附上此部分代码!
CString CharToString(char *chr)
{
CString str;
str=(LPCTSTR)chr;
return str;
}
int WriteStringFile(CString str/*写入文件的字符串*/,const char *filename/*文件名*/,CString strCut/*字符串分割符*/,CString jm/*加密字符串*/)
{
CStdioFile m_file;
m_file.Open(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
CArchive ar(&m_file,CArchive::store);
CStringList m_strList;
int nCount;
while(!str.IsEmpty())
{
int index;
CString str2;
index=str.Find(strCut,0);
if(index!=-1)
{
str2=str.Left(index);
str.TrimLeft(str2);
str.TrimLeft(strCut);
int nLength;
nLength=str2.GetLength();
char *temp1;
temp1=new char[nLength+1];
memset(temp1,0x00,(nLength+1)*sizeof(char));
lstrcpy(temp1,str2);
int nLength1;
nLength1=jm.GetLength();
char *temp2;
temp2=new char[nLength1+1];
memset(temp2,0x00,(nLength1+1)*sizeof(char));
lstrcpy(temp2,jm);
char *ptemp2;
ptemp2=temp2;
char *ptemp1;
ptemp1=temp1;
while(*ptemp1)
{
if(*ptemp2=='\0') ptemp2=temp2;
(*ptemp1)^=(*ptemp2);
ptemp1++;
ptemp2++;
}
CString str3;
str3=CharToString(temp1);
m_strList.AddTail(str3);
if(temp1) delete temp1;
if(temp2) delete temp2;
str2.ReleaseBuffer();
}
}
nCount=m_strList.GetCount();
ar<<nCount;
for(POSITION pos=m_strList.GetHeadPosition();pos;)
{
CString strTemp=m_strList.GetNext(pos);
ar<<strTemp;
}
ar.Close();
m_file.Close();
return 1;
}
CString CharToString(char *chr)
{
CString str;
str=(LPCTSTR)chr;
return str;
}
int WriteStringFile(CString str/*写入文件的字符串*/,const char *filename/*文件名*/,CString strCut/*字符串分割符*/,CString jm/*加密字符串*/)
{
CStdioFile m_file;
m_file.Open(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
CArchive ar(&m_file,CArchive::store);
CStringList m_strList;
int nCount;
while(!str.IsEmpty())
{
int index;
CString str2;
index=str.Find(strCut,0);
if(index!=-1)
{
str2=str.Left(index);
str.TrimLeft(str2);
str.TrimLeft(strCut);
int nLength;
nLength=str2.GetLength();
char *temp1;
temp1=new char[nLength+1];
memset(temp1,0x00,(nLength+1)*sizeof(char));
lstrcpy(temp1,str2);
int nLength1;
nLength1=jm.GetLength();
char *temp2;
temp2=new char[nLength1+1];
memset(temp2,0x00,(nLength1+1)*sizeof(char));
lstrcpy(temp2,jm);
char *ptemp2;
ptemp2=temp2;
char *ptemp1;
ptemp1=temp1;
while(*ptemp1)
{
if(*ptemp2=='\0') ptemp2=temp2;
(*ptemp1)^=(*ptemp2);
ptemp1++;
ptemp2++;
}
CString str3;
str3=CharToString(temp1);
m_strList.AddTail(str3);
if(temp1) delete temp1;
if(temp2) delete temp2;
str2.ReleaseBuffer();
}
}
nCount=m_strList.GetCount();
ar<<nCount;
for(POSITION pos=m_strList.GetHeadPosition();pos;)
{
CString strTemp=m_strList.GetNext(pos);
ar<<strTemp;
}
ar.Close();
m_file.Close();
return 1;
}
每个字符串都加上_T()或TEXT()
譬如加入什么样的头文件,还有我试了一下,说你的“zh-cn”这个字符无效,你能否写清楚一点!!!!
具体些好吗?兄弟!
譬如加入什么样的头文件,还有我试了一下,说你的“zh-cn”这个字符无效,你能否写清楚一点!!!!
#pragma setlocale( "locale-string" )Defines the locale (country and language) to be used when translating wide-character constants and string literals. Since the algorithm for converting multibyte characters to wide characters may vary by locale or the compilation may take place in a different locale from where an executable file will be run, this pragma provides a way to specify the target locale at compile time. This guarantees that the wide-character strings will be stored in the correct format. The default locale-string is "C". The "C" locale maps each character in the string to its value as a wchar_t (unsigned short).
好像不行呀!有没有别的方法,unicode是不是一定要在NT下进行编译,能不能在98 底下编译呢?如果98行的话,对编译环境进行什么样的设置呢?
望告知!!!谢谢!!
#include<tchar.h>
#include <locale.h>setlocale(LC_ALL,_T("chs"));
因为在UNICODE(Win nt,Win2000...)机上能构正常运行,ANSI也能正常运行(速度会慢些)而在ANSI(Win98,Unix...)上UNICODE会出错,ANSI则会出正常
强!神秘罕见的API函数罗列了一大堆
也不看看死循环到底发生在哪里
是哪个while或for死掉了?
循环死了,肯定是循环结束条件永远达不到
于是注意力就应该集中到那个循环条件控制变量上
想想它控制循环结束的条件是什么,这个条件值应该在什么情况下达到,现在为什么达不到?怎样才能达到?
最后,您的while循环比较危险了,比如“while(*ptemp1)”这样的条件就不安全,最好“while(*ptemp1 | 另一个足够安全的备用条件)”。备用条件比如检查循环时间,太长了则超时退出。
我的代码是服务器端用的是linux,客户端用的win98,服务器数据库使用的是mysql,整个服务器端用的是mysql的API c函数编写的,整个数据库操作都是在服务器端进行的,这都是遵照公司头儿的意思写的,也就是说客户端对数据库进行访问是都是通过发一个sql语句,然后在服务器端进行操作,得出结果后再把结果发给客户端,得到的结果是CString类型,居然在内存中能看到汉字,但是进行拆分是循环到这一部分,发现不能对汉字字符进行处理?
我还以为是不是汉字是宽字符的样子吧!我想CString是一个已经封装得很好的类,而且也能处理汉字,所以我又试着把CString串行化到文件中去,居然也是操作失误,出现什么致命错误信息!!而没有通过网络传输或者windowszhijian进行传输的CString的类在windows下是不会有这种低级错误的!!我现在想是不是在从windows传输到linux下时的字符集问题,还是从linux传输到windowsxia时的字符集问题,两者的字符编码是不是有所不同呢?另外mysql数据库的默认字符集时ladin1。
兄弟能否为我解决这个问题,拜托各位了
还有,你的代码也不写注解,别人看起来很头痛。其实,你只要在服务器端和客户端都转成WCHAR来操作(用MultiByteToWideChar()来转,不是要你用UNICODE宏哟)就不会有问题。比如:CString str;
WCHAR* lpWStr = new WCHAR[str.GetLength()+1];
memset( lpWStr,0,2*str.GetLength()+1);
MultiByteToWideChar(...,str,lpWstr....) //先转成WCHARwhile (...)
{
if ( (lpWStr[i] == L'\r')&&(lpWStr[i+1] == L'\n') ) //找回车换行
{
}}
我已经自己找到了问题底所在了,关键在CString类的引用上,我现在把他解决好了,因为我用的是在一个引用上进行切分,而在理论上是可以通过的,但实际应用时并不行,我把他改成从该引用中在切分字符串中去提取自己所需的各个字符串!!谢谢大家!!!为了表示感谢,把分给那些提供了帮助的朋友!!