#include <bits/stdc++.h>
using namespace std;
void ScanFile(string filename);
int main()
{
string filename;
cout << "请输入要扫描的文件名:";
cin >> filename;
ScanFile(filename);
cout << endl << "扫描完成。" << endl;
system("pause");
return 0;
}
void ScanFile(string filename)
{
ifstream file(filename.c_str());
if (!file)
{
cout << "无法打开文件 " << filename << "。" << endl;
system("pause");
}
string line;
while (getline(file, line))
{
if (line.find("virus") != string::npos)
{
cout << "发现病毒!" << endl;
break;
system("pause");
}
}
file.close();
}
本质:检测文件中是否含有“virus”这个字符串。