Thread: need Help to design search Program..????

  1. #1
    Registered User
    Join Date
    Dec 2010
    Lucknow, India

    need Help to design search Program..????

    Hi. I just want to design a program which is able to find the file whose name is taken as input in the HARD DISK.
    Suppose we want to search the file the name as "myfile.txt" then the Program will give the Output as the file exist and its complete position for ex: "D:\my folder\myfile.txt"..
    i dont know from where to start this code..
    My concepts of file handling in c are clear. By using fopen() we can check the existence of the file in a well known folder but I cant decide that how can we take the name of the unknown folders that exist in a Drive.
    Need HELP and please give me some tips in writing this code.
    I'm using Borland C on Windows XP.

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    You will most likely need to delve into windows for this...

    Please note that #include <windows.h> does not mean you have to create a gui program (although you probably should). You can do this in console is you like...

    You will need to use functions like SetCurrentDirectory() , FindFirstFile() and FindNextFile() to scan directories in a recursive manner looking for your file... Nicely FindFirstFile() allows wildcards so that part of your search is greatly simplified.

    Here's an example of directory recursion. It has a few extra bits in it but you should be able to pull out the basics to get you started on your own program...
    // recursive directory search
    void TestDir(PVOID SPath)
      { HANDLE HDir;
        ++ FolderCount;
        if (!SetCurrentDirectory(SPath))
          { PostMessage(WHandle[0],UM_ADDERR,GetLastError(),(LPARAM)SPath);
            return; }
        HDir = FindFirstFile("*.*",&DirData);
        if (HDir == INVALID_HANDLE_VALUE)
          { PostMessage(WHandle[0],UM_ADDERR,GetLastError(),0);
            return; }
          { if (DirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
              {if (Recurse && (DirData.cFileName[0] != '.'))
                 TestDir(DirData.cFileName); } 
              TestFile(DirData.cFileName); }
        while (FindNextFile(HDir,&DirData) && Scanning);
        SetCurrentDirectory(".."); }
    While you're at it you might want to get a better compiler... Borland C is a dinosaur...

    smorgasbordet - Pelles C
    Last edited by CommonTater; 01-14-2011 at 01:15 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Check out your help file for "findfirst()" and "findnext()", and everything else in dir.h (which you'll need to include).

    In general:

    use fgets() to take in the data from the user

    remove the next to the last char newline with something like:
    Then use the names you get from your program, to do a strcmp:
    while(you still have more files) {
      if((strcmp(filenamearray,filename))==0) {  //are they the same?
        print (found it)                                   //yes
        return 1
    print (file not found)
    return 0                                              //no
    This is some Turbo C code that works on Windows XP, (right out of the help examples).
    Put this with some of the above, add a little "logic glue", and you should be well on your way.

    #include <stdio.h>
    #include <dir.h>
    int main(void)
       struct ffblk ffblk;
       int done;
       printf("Directory listing of *.*\n");
       done = findfirst("*.*",&ffblk,0);
       while (!done)
          printf("  %s\n", ffblk.ff_name);
          done = findnext(&ffblk);
       return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a Unix Search Program, some questions
    By Acolyte in forum C Programming
    Replies: 3
    Last Post: 09-23-2008, 12:53 AM
  2. Simple search program
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 12-18-2004, 01:58 AM
  3. search array program
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2002, 07:33 AM
  4. Need Help with 2D Garden Design Program
    By frgmstr in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 04:58 PM