0

search trung in file

how can i search a string in a file?

15th Jan 2017, 4:00 PM
Matin Grimes
Matin Grimes - avatar
1 Answer
+ 3
You can use this as a Base example. #include <iostream> #include <fstream> #include <string> int main() { std::cout << "Write the path of the file\n" ; std::string path ; std::cin >> path ; std::ifstream file( path.c_str() ) ; if( file.is_open() ) { std::cout << "File '" << path << "' opened.\n" ; std::cout << "Write the word you're searching for\n" ; std::string word ; std::cin >> word ; int countwords = 0 ; std::string candidate ; while( file >> candidate ) // for each candidate word read from the file { if( word == candidate ) ++countwords ; } std::cout << "The word '" << word << "' has been found " << countwords << " times.\n" ; } else { std::cerr << "Error! File not found!\n" ; return 1 ; } }
15th Jan 2017, 4:25 PM
Alex
Alex - avatar