1 #include <string> 2 #include <fstream> 3 #include <iostream> 4 5 using namespace std; 6 7 const string sourceFile="source.txt"; //源文件路径 8 const string resultFile="result.txt"; //结果文件路径 9 const string keyStr="a"; //关键字符串 10 11 int _tmain(int argc, _TCHAR* argv[]) 12 { 13 string str; 14 ifstream fin(sourceFile); 15 ofstream fout(resultFile); 16 17 if(fin.fail()) 18 { 19 cout<<"source file open failed!"; 20 return 1; 21 } 22 23 24 while ( getline(fin,str)) //按行读取, 25 { 26 if( str.find(keyStr,0)!=string::npos ) //查找关键字符串, 27 fout<<str<<"\n"; 28 } 29 30 return 0; 31 }