1

I want to get all the files of type A*.txt present in current directory via C++ code. My OS is *Nix system. Also, I want to get the names of such files.

Please suggest how can this be done.

I tried using system command but system doesn't return anything apart a integer which says if command was executed properly or not.

Thanks

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Mayank Jain
  • 2,504
  • 9
  • 33
  • 52
  • possible duplicate of [How to get list of files with a specific extension in a given folder](http://stackoverflow.com/questions/11140483/how-to-get-list-of-files-with-a-specific-extension-in-a-given-folder) – m.s. Jun 29 '15 at 08:32

1 Answers1

3

There are basically three ways you can go about this.

One is to use basically what you tried before, but using the popen function, which allows you to read the output of the command(s) you run.

The second solution is to use e.g. opendir and readdir or scandir to manually filter and find the files you look for.

The third and easiest way is to use the glob function.


There is actually a fourth way as well, one which is platform independent and more C++-ish than the above methods: Using the Boost filesystem library.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621