#include <iostream>
#include<fstream>
using namespace std;
void myFind(const string& filename, const string& tofind);
int main()
{
myFind("/Users/xuconglong/Desktop/text/1.txt", "5");
return 0;
}
void myFind(const string& filename, const string& tofind)
{
ifstream fin(filename.c_str(),ios_base::binary);
string str(1024*1024*2,0);
fin.read(&str[0],2*1024*1024);
fin.close();
string::size_type beg = 0;
string::size_type pos = -1;
string::size_type find_size = tofind.size();
if(0 == find_size)
{
cout << "no such file or no content" << endl;
return;
}
unsigned count = 0;
while((pos=str.find(tofind,beg))!=string::npos)
{
beg=pos+find_size;
count++;
}
cout << "find matched number:" << count << endl;
}
【c++文本文件操作之查找】mac系统xcode编译环境
最新推荐文章于 2023-10-18 11:05:23 发布