文本内容如下:
python源码如下:
-
-
- if __name__ == '__main__':
- f=open('demo.txt')
- line=f.readline()
- while line:
- if 'keyWord' in line:
- print("find keyWord!!!")
- pass
- line = f.readline();
- pass
- f.close()
运行截图如下:
C++源码如下:
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
-
- int main(){
-
- ifstream file;
- file.open("demo.txt");
-
- string line;
- while(getline(file,line)){
- if(line.find("keyWord")!=string::npos){
- cout<<"Find KeyWord!"<<endl;
- }
- }
-
- file.close();
- getchar();
- return 0;
- }
运行截图如下:
Qt源码如下:
- #include <QString>
- #include <QDebug>
- #include <QFile>
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QFile file("E:\\Qt2018\\FindKeyWord\\demo.txt");
- if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
- return -1;
-
- QString line=file.readLine();
- while(!line.isEmpty()){
-
- if(line.contains("keyWord")){
- qDebug()<<"Find keyWord!";
- }
-
- line=file.readLine();
- }
-
- file.close();
-
- return a.exec();
- }
运行截图如下: