c++ - Search a word in a textfile using std instructions -


i think that's not difficult question want know how in c++ way. want search position of defined word or sequence.

i read post stack similar question , answer looks great. don't know how apply solution file. std instruction it's easy apply in or don't know how apply file , sequence.

instruction:

std::ifstream file; std::search(std::istreambuf_iterator<char>(file.rdbuf()) // search here           , std::istreambuf_iterator<char>()             // ... here           , psignature                                   // looking sequence starting           , psignature+sigsize);                         // , ending 

can use string store sequence search in file ???

could post easy example of how apply search instruction, obatin , big error when compile it.

i don't use windows , don't want use boost library.

thanks in advance.

read file string (assuming it's not huge). can use string::find or std::algorithm.

using namespace std;  // read entire file string ifstream file(".bashrc"); string contents((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());  string needle = "export";  // search using string::find size_t pos = contents.find(needle); cout << "location using find: " << contents.find(needle) << endl;  // search using <algoritm>'s search string::const_iterator = search(contents.begin(), contents.end(), needle.begin(), needle.end()); cout << "location found using search: " << string(it, + 10) << endl; cout << "    @ position: " << - contents.begin() << endl; 

[edit] can search istreambug_iterators directly, leaves same kind of iterator.

istreambuf_iterator<char> it2 = search(         istreambuf_iterator<char>(file), istreambuf_iterator<char>(),         needle.begin(), needle.end()); 

Comments