I need to check if a file in the format *recipient exists in a directory and if it does get its name. I tried with using opendir() and readdir() to list each and every file on a directory and compare it but in cases where there are a lot of files this is time consuming.
Is there any better way to achieve this? If yes, do you have small snippet showing how to do this?
Thank you, code is appreciated.
EDIT:
Just to be more clear, I need to check a directory for any file that ends in recipient or otherwise put *recipient if i were to use ls, if a file with that name exists then I need to get its name.
The routine
globdoes just what you’re looking for. Usage examples are available here:http://www.opengroup.org/onlinepubs/009695399/functions/glob.html
Here’s a full example that should get you what you need: