Searching For Files

This is a discussion on Searching For Files within the C++ Programming forums, part of the General Programming Boards category; Hi all. I was writing this program to search for/ list files, and I came across a rather annoying problem. ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Searching For Files

    Hi all.

    I was writing this program to search for/ list files, and I came across a rather annoying problem. I'll post the code first:

    Code:
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <fstream>
    #include <process.h>
    
    using namespace std;
    
    #define nul '\0'
    
    char Stop_Search = nul;
    
    void KeyCheck (void *);
    
    bool Search_For_Specified_File();
    bool LAFOAST ();
    
    int main()
    {
        char Menu_Selection;
        
        cout<<"\n\n\n    Welcome to the File Locater. This program is able to find and list all\n"
    "    files of a specified type, and can be useful if you wish to check for a\n"
    "    file existance.\n\n    Do you wish to:\n\n"
    "    1. Search for a specified file.\n"
    "    2. List all files of a specified type.\n\n"
    "    Number of desired selection:    ";
        Menu_Selection = getch();
        
        while ( Menu_Selection != '1' && Menu_Selection != '2')
        {
            Menu_Selection = getch();
        }
        
        cout<< Menu_Selection;
        Sleep(2000);
        
        system("cls");
        
        switch (Menu_Selection)
        {
            case '1':
                cout<<"\n\n\n    Search for specified file function goes here.";
                Sleep(2000);
                break;
                
            case '2':
                LAFOAST();
                break;
                
            default:
                cout<<"\n\n\n    Unknown error has occured.\n    Execution terminated.";
                Sleep(2000);
                break;
        }
    }
    
    bool LAFOAST ()
    {
        char Search_Filename = nul;
        string Filename;
        string Extension;
        
        int Files_Found = 0;
        int Character_Number = 0;
        
        cout<<"\n\n\n    Please input the extension of files you wish to list.\n"
    "    If you wish to list all files, leave the following form blank.\n\n"
    "    File Extension:    ";
        
        getline(cin, Extension);
        
        system("cls");
        
        cout<<"\n\n\n    Beginning search...\n"
    "    Press any key to terminate the search.\n\n\n    ";
        
        _beginthread( KeyCheck, 0, NULL );
        
        Sleep(2000);
        
        while (Stop_Search == nul)
        {
            Filename = Extension;
            
            ifstream Read (Filename.c_str());
            
            if (Read.is_open())
            {
                cout<<Filename<<"\n    ";
                Files_Found++;
            }
            
            Search_Filename++;
            Character_Number++;
            
            reverse(Filename.begin(), Filename.end());
            
            Filename += Search_Filename;
            
            reverse(Filename.begin(), Filename.end());
            
            Filename = "";
        }
        
        cout<<"\n    Search Successful\n    Press any key to quit.\n\n    ";
        
        char Quit_Input;
        
        Quit_Input = getch();
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    
    void KeyCheck (void *P)
    {
            Stop_Search = getch();
    }
    My problem is the while loop in LAFOAST. As you probably could have guessed, I'm trying to check the existance of files, by trying all possible combinations until the user stops the search. Trouble is, it checks every combination of one character, followed by the designated extension, then does it again. Is there a way of adding a character infront of the constantly changing one, that changes every time the changing one reaches the final character?

    I can't for the life of me, think of a way to do this by adding to the while loop. Is this possible?
    Any help greatly appriciated.
    Necrofear.
    Last edited by Salem; 01-21-2007 at 10:19 AM. Reason: Fold long lines

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    You don't set te KeyCheck or Stop_Search to not null, it won't stop...

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    I'm a bit confused.

    Could you please elaborate?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Why are you trying to generate every possible filename, rather than just getting a list of the actual files you have (see the FAQ), then seeing which match the conditions you're testing for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 01:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21