#include <iostream>
#include <fstream>
using namespace std;
int main() {
int i,counter = 0;
ifstream readfile;
char filename[60];
char text[60],ch;
cout << "待查找的文件名:";
cin >> filename;
readfile.open(filename);
if(readfile.is_open == false) {
cout << "没有找到文件:" << filename << endl;
return 1;
}
while(cin.get() != '\n')
;
cout << "待查找的关键字:";
cin.getline(text,60);
i = 0;
while(readfile >> ch) {
if(ch == text[i]) {
for(++i; text[i] && (readfile >> ch); ++i)
if(text[i] != ch) break;
if(text[i] == '\0') ++counter;
if(ch == text[0]) i = 1;
else i = 0;
}
}
cout << "\n文 件 名: " << filename << endl;
cout << "关 键 字: " << text << endl;
cout << "出现次数: " << counter << endl;
return 0;
}
追问如果是给定文件001.txt和002.txt等查找的话应该怎么写啊,
追答运行后见到提示"待查找的文件名:"时,输入相应的文件名即可。
追问那可不可以写代码合在一起呢。如结果:输出关键字在001.txt出现多少次,在002.txt出现多少次,你QQ多少啊我可以加你吗?
追答876929541
追问你怎么不回我呢0.0总之就是怎么同时把001.txt和002.txt放进去最后输出结果呢