-6

What method (or a library function) do we use in C or C++ to read the file index ( i.e., the list of all the files) in a directory.

My motive is to create a function to make an extended search possible in a directory (or a file system) by reading the list of files in a directory. Since we don't already know what files are existing on the disk we cannot open and read them.

However, I know that programs like 'ls' and 'dir' in Unix/Linux are able to read the contents of a directory; since these OS are also written mostly in C, there must be some way to create a similar function under one program.

Following is the algorithm that I want to use:

  1. Take input from the user to navigate to a directory on the file system
  2. Read the contents of the file
  3. Make a linked list and add to it the contents of the directory that we just read
  4. Use the linked list to read the file name and one by one search through file contents using an existing algorithm
  5. Writing the output to the screen explaining the results found (results are indexed in an order or priority decided by an existing algorithm).

I have coded the algorithms used in step 4 and 5, and can do the step 1 and 3 on my own. The only problem that I'm having is with step 2. Is there any librairy in C/C++ or any other method by which we can achive the solution to this problem?

Thank you!

Chetan Bhasin
  • 3,503
  • 1
  • 23
  • 36
  • 6
    C++ nor C offer such facilities, you need to find and use an appropriate library, such as Boost.Filesystem. – user1095108 Jul 09 '13 at 12:49
  • 1
    Or wait for C++14 which will come along with `filesystem` :-) – Nawaz Jul 09 '13 at 12:58
  • BTW a disk partition might not have any file. Some software are using raw disk partitions. Technically, partitions may contain file systems, and these contain files. – Basile Starynkevitch Jul 09 '13 at 12:59
  • Never knew of something like Boost. I'll definitely try that. – Chetan Bhasin Jul 09 '13 at 13:03
  • I have just edited the question to be more descriptive. – Chetan Bhasin Jul 10 '13 at 08:48
  • C++ does offer methods to read file contents but in a filesystem you have different types of files of various formats. Are you targeting any specific format and contents within it or just looking to read the file name..? – NREZ Jul 11 '13 at 11:31
  • I'm looking to read only the filename. – Chetan Bhasin Jul 11 '13 at 11:35
  • Alright @ChetanBhasin let me know if [this](http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c) could be helpful to you... – NREZ Jul 11 '13 at 11:39

3 Answers3

2

Update#1: Adapting to the edited question

Knowing the path of your directory you can use opendir() to open the directory. Then you read the directory through readdir(). You retrieve what you want and afterwards you close the directory with closedir(). Note that these are system calls for UNIX. So your code would won't be portable. In any case in this header file (dirent.h) you can find all necessary functions in order to deal with directories.

Original answer:

What may work is using exec() system call. You would typically call the underlining system call which does the job for you and collect the results in your program. In this case you are using Linux (according the tag) so you may use ls, find, grep programs which can list your files. Examples can be found here.

KiaMorot
  • 1,668
  • 11
  • 22
  • That's great. I just read about readdir() function, and I think it should work in most cases. I would surely use it for Unix systems, however I'm still looking for something which will work cross-platform. – Chetan Bhasin Jul 11 '13 at 11:37
0

On Linux (and some Posix systems) you may want to use the nftw(3) library function from C.

Of course, there is also the find(1) utility command, usable from a shell.

Maybe you want also the readdir(3) function and the stat(2) syscall.

And as several commented, many libraries could help you, e.g. in C++: boost, Poco libraries, QtCore, and in C: glib (from Gtk) and others.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

When browsing through the forum, I found this question which was similar to mine.

How can I get the list of files in a directory using C or C++?

I think that answers my problem.

Community
  • 1
  • 1
Chetan Bhasin
  • 3,503
  • 1
  • 23
  • 36