Thread: Program does not find any matching file

  1. #1
    Registered User
    Join Date
    Jul 2004

    Program does not find any matching file

    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 ?
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <windows.h>
    int main(int argc, char** argv) {
        WIN32_FIND_DATA wfd;
        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;
                                          f_handle.close( );
        } while(FindNextFile(handle, &wfd) != false);
        std::cin.get( );

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    you can do:
    while(std::getline(std::cin, temp))
    wihtout all the if (f_handle.is_open()) and the f_handle.peek().. oh, wait, I see what your issue is. you are getting the value of temp from std::cin, you should be passing f_handle to it instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Why isn't my program reading my file properly?
    By DCMann2 in forum C Programming
    Replies: 10
    Last Post: 04-23-2008, 02:16 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of
    Replies: 2
    Last Post: 10-19-2006, 11:08 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM
Website Security Test