HasStringInFile

From Yefu's notes
Jump to: navigation, search
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator>
#include <algorithm>

static inline bool HasStringInFile(const string& in_fname, const string& in_patten)
{
	ifstream fs(in_fname);
	fs.unsetf(ios::skipws); 
	istream_iterator<char> file(fs), eof;
	
	stringstream ss;
	ostream_iterator<char> memory(ss);
	copy(file, eof, memory);
	fs.close();
	string& filestr = ss.str();
	
	return filestr.end() != search(filestr.begin(), filestr.end(), in_patten.begin(), in_patten.end());
}