Hey guys. I want my program to output all the files, in a given folder, that contains a word entered by the user. What the program does, though, is simply output ALL the file names, even though it's obvious that they don't all contain the string since 3 of the files in the directory are images so and 1 of the 3 text files does not contain the word. Can you guys help me ?
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
int main(int argc, char** argv) {
WIN32_FIND_DATA wfd;
HANDLE handle = INVALID_HANDLE_VALUE;
std::string path;
std::cout << "Chemin du fichier: ";
std::getline(std::cin, path, '\n');
std::string word;
std::cout << "Mot ou expression: ";
std::getline(std::cin, word, '\n');
handle = FindFirstFile(path.c_str( ), &wfd);
do {
if(handle == INVALID_HANDLE_VALUE) break;
if(wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) continue;
std::cout << wfd.cFileName;
std::ifstream f_handle(wfd.cFileName);
if(f_handle.is_open( )) {
std::string temp;
int counter = 1;
while(f_handle.peek() != EOF) {
std::getline(std::cin, temp, '\n');
if(temp.find(word.c_str( )) != std::string::npos) {
std::cout << wfd.cFileName << '\t' << counter << std::endl;
break;
}
counter++;
}
f_handle.close( );
}
} while(FindNextFile(handle, &wfd) != false);
std::cin.get( );
}