本人菜鸟,现写了一段代码,目的是打开多个文本文件,读取其内容并统计搜索到关键字的个数,代码如下:
char szFilters[] = "Test Files (*.test)|*.*|| ";
CFileDialog dlg(TRUE,"*","",OFN_NOVALIDATE|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,szFilters,this);
dlg.m_ofn.lpstrTitle = "Select Files ";
if (dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
int i=0;
char *pBuf;
DWORD dwFileLen;
while (pos)
{
CString strPath = dlg.GetNextPathName(pos);
strPath = strPath.Right(27);
CFile file(strPath,CFile::modeRead);
dwFileLen = file.GetLength();
pBuf = new char[dwFileLen+1];
pBuf[dwFileLen] = 0;
file.Read(pBuf,dwFileLen);
file.Close();
char ch1[512] = {0};
GetDlgItem(IDC_EDIT_IMKEY)->GetWindowText(ch1,100);
char *FK = strstr(pBuf,ch1);
if (FK == NULL)
{
AfxMessageBox(strPath);
continue;
}
else
{
i++;
}
}
CString std;
std.Format("%d",i);
AfxMessageBox(std);
问题来了,打开6个以下的文件,可以正常搜索出结果,但6个以上时,就报了个错,DeBug进去发现strPath值为问号,望各位大虾不吝赐教,谢谢
char szFilters[] = "Test Files (*.test)|*.*|| ";
CFileDialog dlg(TRUE,"*","",OFN_NOVALIDATE|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,szFilters,this);
dlg.m_ofn.lpstrTitle = "Select Files ";
if (dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
int i=0;
char *pBuf;
DWORD dwFileLen;
while (pos)
{
CString strPath = dlg.GetNextPathName(pos);
strPath = strPath.Right(27);
CFile file(strPath,CFile::modeRead);
dwFileLen = file.GetLength();
pBuf = new char[dwFileLen+1];
pBuf[dwFileLen] = 0;
file.Read(pBuf,dwFileLen);
file.Close();
char ch1[512] = {0};
GetDlgItem(IDC_EDIT_IMKEY)->GetWindowText(ch1,100);
char *FK = strstr(pBuf,ch1);
if (FK == NULL)
{
AfxMessageBox(strPath);
continue;
}
else
{
i++;
}
}
CString std;
std.Format("%d",i);
AfxMessageBox(std);
问题来了,打开6个以下的文件,可以正常搜索出结果,但6个以上时,就报了个错,DeBug进去发现strPath值为问号,望各位大虾不吝赐教,谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货