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


 
Thread Tools Search this Thread
Top Forums Programming Program to search for a file on disk using c++
# 1  
Old 01-05-2012
Java 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

Smilie
# 2  
Old 01-05-2012
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.
This User Gave Thanks to pflynn For This Post:
# 3  
Old 01-06-2012
Bug Thank you

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

Thanks
# 4  
Old 01-06-2012
Having been told about opendir() and readdir(), you already have plenty to go on.
Code:
$ 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:

Code:
$ 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:

Code:
#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);
}

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-07-2012
MySQL Thank you

Thank you very much, this helps a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Program to search for the files with specific format

Hi All, Your help on the below requirement is highly appreciated. I am building a program which should pick the file from a source directory and place them into target directory. While selecting the file in source it should look for some pattern in the file name. Ex: File Name 1:... (10 Replies)
Discussion started by: bharath561989
10 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Retrieving the relevant search from search file in the main file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search... (7 Replies)
Discussion started by: csim_mohan
7 Replies

4. Programming

C program to read a binary file and search for a string?

Hi, I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language book:o At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

6. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

7. Programming

Writing a search, copy and paste program

Hello, Can some one help me by writing me the script of a program that does the following simple functions on a Linux Mint: 1. it runs in the background all the time, doing nothing except of checking if there is any external device mounted. 2. when the device is detected, it copies files... (1 Reply)
Discussion started by: Black_Ardilla
1 Replies

8. Homework & Coursework Questions

C Program to search and read all named pipes in current directory

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a C program to search the current directory for all pipes. 1. It will print the pipe... (2 Replies)
Discussion started by: natwickley
2 Replies

9. Shell Programming and Scripting

Kill shell script when host program not running/disk unmounted

I have my Mac OS X program executing a shell script (a script that copies files to a drive). I want to make it so that the shell script automatically kills itself if it finds that the host .app is not running OR kill itself if the drive that it is copying files to has been unmounted. Right now what... (2 Replies)
Discussion started by: pcwiz
2 Replies

10. Programming

How to get free disk space size in C/C++ program( Solaris system)

How to get free disk space size in C/C++ program( Solaris system)? Is there any standard function or system function? Just like "df" or "getdfree" in Linux. (4 Replies)
Discussion started by: yidu
4 Replies
Login or Register to Ask a Question