61.C++文件操作实现硬盘检索

 1 #include <iostream>
 2 #include <fstream>
 3 #include <memory>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <cstring>
 7 using namespace std;
 8 
 9 void main()
10 {
11     ifstream fin("F:\大数据\大数据相关数据\kaifang.txt");
12     ofstream fout(R"(C:
es.txt)");
13     if (!fin || !fout)
14     {
15         cout << "文件打开失败" << endl;
16         return;
17     }
18     //如果没有到文件末尾
19     while (!fin.eof())
20     {
21         //C语言方式查找
22         //char str[500];
23         //fin.getline(str, 500);
24         //char *p = strstr(str, "小王");
25         //if (p != nullptr)
26         //{
27         //    //输出到屏幕
28         //    cout << str << endl;
29         //    //写入到文件
30         //    fout << str << endl;
31         //}
32 
33         //C++方式查找
34         char strpos[500]{ 0 };
35         //读取一行
36         fin.getline(strpos, 500);
37         string str = strpos;
38         //从第0个位置开始查找
39         int pos = str.find("小李",0);
40         if (pos != -1)
41         {
42             cout << str << endl;
43             fout << str << endl;
44         }
45     }
46 
47     fin.close();
48     fout.close();
49     cin.get();
50 }
原文地址:https://www.cnblogs.com/xiaochi/p/8567680.html