Program to search for a file on disk using c++

Hey guy,

Is it possible to write a program in c++ to search for file in a particular drive?
If so please provide the logic or the code

Thanks

:slight_smile:

You have options that range from opening directories and reading their contents manually ( opendir, readdir), searching for the files (and doing it recursively) to using more specific functions like scandir, available in many systems, but with slightly different interfaces.

1 Like

Could you please post an algorithm or code please? I am new at programming

Thanks

Having been told about opendir() and readdir(), you already have plenty to go on.

$ man 3 opendir

OPENDIR(3)                 Linux Programmer's Manual                OPENDIR(3)



NAME
       opendir, fdopendir - open a directory

SYNOPSIS
       #include <sys/types.h>
       #include <dirent.h>

       DIR *opendir(const char *name);
       DIR *fdopendir(int fd);

...

SEE ALSO
       open(2),  closedir(3),  dirfd(3), readdir(3), rewinddir(3), scandir(3),
       seekdir(3), telldir(3)

$ man 3 readdir

READDIR(3)                 Linux Programmer's Manual                READDIR(3)



NAME
       readdir, readdir_r - read a directory

SYNOPSIS
       #include <dirent.h>

       struct dirent *readdir(DIR *dirp);

       int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

           struct dirent {
               ino_t          d_ino;       /* inode number */
               off_t          d_off;       /* offset to the next dirent */
               unsigned short d_reclen;    /* length of this record */
               unsigned char  d_type;      /* type of file; not supported
                                              by all file system types */
               char           d_name[256]; /* filename */
           };

Though stat and ftw are also useful for what you want:

$ man ftw

STAT(2)                    Linux Programmer's Manual                   STAT(2)



NAME
       stat, fstat, lstat - get file status

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

       int stat(const char *path, struct stat *buf);
       int fstat(int fd, struct stat *buf);
       int lstat(const char *path, struct stat *buf);

...

       All  of  these system calls return a stat structure, which contains the
       following fields:

           struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

       The st_dev field describes the device on which this file resides.  (The
       major(3)  and  minor(3) macros may be useful to decompose the device ID
       in this field.)

$ man ftw

FTW(3)                     Linux Programmer's Manual                    FTW(3)



NAME
       ftw, nftw - file tree walk

SYNOPSIS
       #include <ftw.h>

       int ftw(const char *dirpath,
               int (*fn) (const char *fpath, const struct stat *sb,
                          int typeflag),
               int nopenfd);

...


       For  each  entry  found  in the tree, ftw() calls fn() with three argu-
       ments: fpath, sb, and typeflag.  fpath is the pathname  of  the  entry,
       and is expressed either as a pathname relative to the calling process's
       current working directory at the time of the call to ftw(), if  dirpath
       was  expressed  as  a relative pathname, or as an absolute pathname, if
       dirpath was expressed as an absolute pathname.  sb is a pointer to  the
       stat structure returned by a call to stat(2) for fpath.

...

      To stop the tree walk, fn() returns a nonzero value;  this  value  will
       become  the  return  value  of ftw().  As long as fn() returns 0, ftw()
       will continue either until it has traversed the entire tree,  in  which
       case  it  will  return zero, or until it encounters an error (such as a
       malloc(3) failure), in which case it will return -1.

[/code]

so:

#include <sys/types.h>
#include <sys/stat.h>
#include <ftw.h>

void die(char *msg)
{
        perror(msg);
        exit(1);
}

int maj, min; //. what drive to stay inside

int search(const char *fpath, const struct stat *sb, int typeflag)
{
        // Ignore things not in the right folder
        if((major(sb->st_dev) != maj) || (minor(sb->st_dev) != min))
                return(0);

        printf("%s\n", fpath);
}

int main(void)
{
        const char *base="/path/to/folder";
        struct stat sbuf;

        if(stat(base, &sbuf) < 0) die("Couldn't stat base");

        maj=major(sbuf.st_dev); min=minor(sbuf.st_dev);

        ftw(base, search, 15);
        return(0);
}
1 Like

Thank you very much, this helps a lot :slight_smile: