vc2005 + win2000.//VC6下没有问题实际运行时提示 "An invalid argument was encountered.",我查了两天多.哪位能帮测试一下,看看是什么问题.谢谢!
代码在下面链接中.//我只把出错的抽出来.http://www.shuct.net/down.asp?id=20110421180927&check=E40C8C483358CCE01517322E61E825FC
......
CString ls_swap;theApp.loadfile("1111.txt",&ls_swap);MessageBox(NULL,ls_swap,"",NULL);//到这里没问题
ls_swap.MakeLower();//这一句出错
MessageBox(NULL,ls_swap,"",NULL);
......---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Runtime Error!Program: f:\desktop\...This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
---------------------------
确定
---------------------------
......
代码在下面链接中.//我只把出错的抽出来.http://www.shuct.net/down.asp?id=20110421180927&check=E40C8C483358CCE01517322E61E825FC
......
CString ls_swap;theApp.loadfile("1111.txt",&ls_swap);MessageBox(NULL,ls_swap,"",NULL);//到这里没问题
ls_swap.MakeLower();//这一句出错
MessageBox(NULL,ls_swap,"",NULL);
......---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Runtime Error!Program: f:\desktop\...This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
---------------------------
确定
---------------------------
......
我在我这里测试,内容是你提供的那个 1111.txt ,我没修改。然后我读取进来,格式化为字符串,然后显示,正常。
//读取"111.txt" 文件
CFile _file;
if(!_file.Open((LPCTSTR)"1111.txt",CFile::modeRead))
{
AfxMessageBox((LPCTSTR)"打开文件失败!");
return ;
}
//文件长度;
int _len = _file.GetLength();
//已经处理的长度
int _lendone=0;
int testprocess=0;
char* pdata = new char[_len+1];
char *strtem=new char[256];
//-----------------------------------------=
//读文件;
_file.Read(pdata,_len);
pdata[_len] = 0;
_file.Close(); str.Format("%s",pdata);
MessageBox(str,"原始的1111.TXT",MB_ICONEXCLAMATION);//正常
str.MakeLower();
MessageBox(str,"测试MakeLower",MB_ICONEXCLAMATION);//正常
str.MakeUpper();
MessageBox(str,"测试MakeUpper",MB_ICONEXCLAMATION);//正常不做任何处理,直接显示,正常;
把所有字母转为小写,显示,正常;
大所有字母转为小写,显示,正常;
我感觉,不是你所说的哪一行出错,应该是其他地方出错了。而且,我运行你这个程序,是在程序关闭后,才出现那个错误的。我的是 XP 系统
这个应该不会出错吧,你说说的特殊字符串下,makelower会出错,那你提供这个“特殊”的字符串给我测试下?我下载了你提供的那个 1111.txt ,不修改,用这些转换函数,完全正常的。
去掉就正确了
CString str;
str += (char)-87;
str.MakeLower();CString 中含有负数的话,MakeLower就会抛出异常
if(!_file.Open((LPCTSTR)"1111.txt",CFile::modeRead))
{
AfxMessageBox((LPCTSTR)"打开文件失败!");
return false;
}
//文件长度;
int _len = (long)_file.GetLength();
//已经处理的长度
int _lendone=0;
int testprocess=0;
char* pdata = new char[_len+1];
char *strtem=new char[256];
//-----------------------------------------=
//读文件;
_file.Read(pdata,_len);
pdata[_len] = 0;
_file.Close();
CString str;//这个是我加上来的,您原来的代码中没有 str.Format("%s",pdata);
MessageBox(NULL,str,"原始的1111.TXT",MB_ICONEXCLAMATION);//正常
str.MakeLower();
MessageBox(NULL,str,"测试MakeLower",MB_ICONEXCLAMATION);//正常
str.MakeUpper();
MessageBox(NULL,str,"测试MakeUpper",MB_ICONEXCLAMATION);//正常
return false;还是会
那个文件长度是2294,用CharLower后,是2293......我是不是只能自己重新写一个MakeLower?谢谢!
是你错误的把cstring 当做 char来操作的问题
你在loadfile中破坏了ls_swap 它所封装的 字符串 指针,
不是的,应该是9楼的原因.loadfile是随手写的,跟这个错误没有关系//为的是大家看我程序的时候,可以直接读主要的地方.不用再去写一个函数读入那个字符串.
你的loadfile里怎么实现的?
它只是把 1111.txt的内容读到 ls_swap中,相信和出错没有关系.您也可以另外写一个读取1111.txt的函数.谢谢!
*as_body=*as_body+lc 改变了as_body 所封装的char* 或者是 wchar*,离开作用域后,as_body 也就是ls_swap 里面的值无效对他makelower
不错才怪
看看cwnd::getwindowText(Cstring&) 如何写的??void CWnd::GetWindowText(CString& rString) const
{
ASSERT(::IsWindow(m_hWnd));#ifndef _AFX_NO_OCC_SUPPORT
if (m_pCtrlSite == NULL)
{
#endif
int nLen = ::GetWindowTextLength(m_hWnd);
::GetWindowText(m_hWnd, rString.GetBufferSetLength(nLen), nLen+1);
rString.ReleaseBuffer();#ifndef _AFX_NO_OCC_SUPPORT
}
else
{
m_pCtrlSite->GetWindowText(rString);
}
#endif
}
我的代码中没有用到GetBuffer, ReleaseBuffer,具体的问题,烦参见9楼.谢谢!