C++和API结合生成的程序文字显示有关问题
C++和API结合生成的程序文字显示问题
这个单词查询的功能在控制台下运行一切正常,但是结合API用windows窗口后,却发生中断.
功能代码如下:
int len=GetWindowTextLength(GetDlgItem(hDlg,IDC_EDIT));
if( len> 0 )
{
std::ifstream infile;
infile.open( "TextQuery.cpp ");
if( !infile )
{
::MessageBox(NULL,TEXT( "不能打开文件 "),TEXT( "Warning "),MB_OK | MB_ICONWARNING);
return EXIT_FAILURE;
}
TextQuery tq;
tq.ReadFile(infile);
char * buf;
buf=(char *)GlobalAlloc(GPTR,len+1);
GetDlgItemText(hDlg,IDC_EDIT,(LPWSTR)buf,len+1);
std::set <TextQuery::line_no> locs=tq.RunQuery(buf);
typedef std::set <TextQuery::line_no> line_nums;
line_nums::size_type size=locs.size();
TCHAR lable[50];
wsprintf(lable,L "%s occours %d times ",buf,size);
SendDlgItemMessage(hDlg,IDC_LIST,LB_ADDSTRING,0,(LPARAM)lable);
line_nums::const_iterator iter=locs.begin();
while(iter!=locs.end())
{
int index;
TCHAR text[60];
wsprintf(text,L "(line %d) %s ",(*iter)+1,tq.TextLine(*iter));
index=(int)SendDlgItemMessage(hDlg,IDC_LIST,LB_ADDSTRING,0,(LPARAM)text);
SendDlgItemMessage(hDlg,IDC_LIST,LB_SETITEMDATA,(WPARAM)index,(LPARAM)size);
}
GlobalFree((HANDLE)buf);
--------------------------------------|
当我在IDC_EDIT里输入需要查找的字符后,点击IDC_OK,然后就出现中断:
extern "C " int __cdecl _chvalidator(
int c,
int mask
)
{
_ASSERTE((unsigned)(c + 1) <= 256);
return _chvalidator_l(NULL, c, mask);
}
是在我ReadFile时调用BuildMap发生的:
void TextQuery::BuildMap()
{
for(line_no line_num=0; line_num!=lines_of_text.size(); ++line_num)
{
// we 'll use line to read the text a word at a time
istringstream line(lines_of_text[line_num]);
string word;
while( line> > word )
{
// add this line number to the set;
// subscript will add word to the map if it 's not already there
word_map[CleanupStr(word)].insert(line_num);
}
}
}
我想:是不是因为unicode 和ASCII的差别的原因?还是打开文件的模式问题?而且前天也有个这样的程序,读取文本文件后,在windows窗口显示出来的是乱麻,我怀疑是不是类似的原因~
如果有人懂的,麻烦赐教,真诚感谢!
------解决方案--------------------
你的代码间接调用了一些asc II的函数(如isspace),这些函数发现输入的字符不是有效的asc码.
建议你把string/ifstream换成wstring/wifstream,用wide char应该就没有这个问题了
这个单词查询的功能在控制台下运行一切正常,但是结合API用windows窗口后,却发生中断.
功能代码如下:
int len=GetWindowTextLength(GetDlgItem(hDlg,IDC_EDIT));
if( len> 0 )
{
std::ifstream infile;
infile.open( "TextQuery.cpp ");
if( !infile )
{
::MessageBox(NULL,TEXT( "不能打开文件 "),TEXT( "Warning "),MB_OK | MB_ICONWARNING);
return EXIT_FAILURE;
}
TextQuery tq;
tq.ReadFile(infile);
char * buf;
buf=(char *)GlobalAlloc(GPTR,len+1);
GetDlgItemText(hDlg,IDC_EDIT,(LPWSTR)buf,len+1);
std::set <TextQuery::line_no> locs=tq.RunQuery(buf);
typedef std::set <TextQuery::line_no> line_nums;
line_nums::size_type size=locs.size();
TCHAR lable[50];
wsprintf(lable,L "%s occours %d times ",buf,size);
SendDlgItemMessage(hDlg,IDC_LIST,LB_ADDSTRING,0,(LPARAM)lable);
line_nums::const_iterator iter=locs.begin();
while(iter!=locs.end())
{
int index;
TCHAR text[60];
wsprintf(text,L "(line %d) %s ",(*iter)+1,tq.TextLine(*iter));
index=(int)SendDlgItemMessage(hDlg,IDC_LIST,LB_ADDSTRING,0,(LPARAM)text);
SendDlgItemMessage(hDlg,IDC_LIST,LB_SETITEMDATA,(WPARAM)index,(LPARAM)size);
}
GlobalFree((HANDLE)buf);
--------------------------------------|
当我在IDC_EDIT里输入需要查找的字符后,点击IDC_OK,然后就出现中断:
extern "C " int __cdecl _chvalidator(
int c,
int mask
)
{
_ASSERTE((unsigned)(c + 1) <= 256);
return _chvalidator_l(NULL, c, mask);
}
是在我ReadFile时调用BuildMap发生的:
void TextQuery::BuildMap()
{
for(line_no line_num=0; line_num!=lines_of_text.size(); ++line_num)
{
// we 'll use line to read the text a word at a time
istringstream line(lines_of_text[line_num]);
string word;
while( line> > word )
{
// add this line number to the set;
// subscript will add word to the map if it 's not already there
word_map[CleanupStr(word)].insert(line_num);
}
}
}
我想:是不是因为unicode 和ASCII的差别的原因?还是打开文件的模式问题?而且前天也有个这样的程序,读取文本文件后,在windows窗口显示出来的是乱麻,我怀疑是不是类似的原因~
如果有人懂的,麻烦赐教,真诚感谢!
------解决方案--------------------
你的代码间接调用了一些asc II的函数(如isspace),这些函数发现输入的字符不是有效的asc码.
建议你把string/ifstream换成wstring/wifstream,用wide char应该就没有这个问题了