/////////////////////////////////////////////////////////////// // MT2-Q2b - child thread searches text file // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2018 // /////////////////////////////////////////////////////////////// #include #include #include #include #include bool textSearch(const std::string& fileName, const std::string& text) { std::ifstream in(fileName); if (!in.good()) { std::cout << "\n can't open file \"" << fileName.c_str() << "\""; return false; } std::string line; while (std::getline(in, line).good()) { if (line.find(text) != std::string::npos) { in.close(); return true; } } in.close(); return false; } void showResult(bool result, const std::string& filename, const std::string& text) { if (result) std::cout << "\n file \"" << filename << "\" contains text \"" << text << "\""; else std::cout << "\n file \"" << filename << "\" does not contain text \"" << text << "\""; } int main() { std::cout << "\n MT2-Q2b - asynch text search in file"; std::cout << "\n --------------------------------------"; std::string fileName1 = "MT2-Q2b.cpp"; std::string text1 = "async"; std::future fut1 = std::async(std::launch::async, textSearch, fileName1, text1); std::string fileName2 = "../MT2-Q2/MT2-Q2.cpp"; std::string text2 = "feebar"; std::future fut2 = std::async(std::launch::async, textSearch, fileName2, text2); showResult(fut1.get(), fileName1, text1); showResult(fut2.get(), fileName2, text2); std::cout << "\n\n"; }