-2

I am working on a small project in C (Just C not ++ or #) and I was wondering if there was a way that any of you knew of to scan a file/s (Say just its name and or extension)?

Thanks for any help that you can give.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
  • 6
    We have no idea what you mean by "scan". – David Schwartz Oct 22 '13 at 17:46
  • possible duplicate of [How do you get a directory listing in C?](http://stackoverflow.com/questions/12489/how-do-you-get-a-directory-listing-in-c) – Alexander Vogt Oct 22 '13 at 17:51
  • You don't know what scan means? ...I though that was common terminology. I mean like interrogate a file for its name and extension. Something along those lines. – user2908228 Oct 22 '13 at 17:51
  • Provide some examples of the files you want to "scan" and make it clear exactly what information you require from each file – Digital Trauma Oct 22 '13 at 17:54
  • @user2908228 If you don't already know the name and extension, then you don't know what file you want to scan. In that case, how can you do it? It might make sense to "scan" a file to get its file size, last modified date, owner, and so on. But to do anything to a file, you'd have to already know its name. – David Schwartz Oct 22 '13 at 17:56

2 Answers2

1

You can read the content of a folder (in the following example the current working example) using this code that will print all files (and folder):

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>

int main (void) {

      DIR *dp;
      struct dirent *ep;
      dp = opendir(".");   // open the current directory
      if (dp != NULL) {
        while ((ep = readdir(dp)) != NULL) {     // read its content one by one
           printf("%s\n", ep->d_name);
      }
      closedir(dp);   // close the handle
     }
      else
      perror("Can not access dir");
return 0;
}

You can of course parse the individual file names for their extension in a next step. Note that this example is for Linux.

PhillipD
  • 1,797
  • 1
  • 13
  • 23
  • Is there any way to save the directory contents to a variable or array using this method? – user2908228 Oct 23 '13 at 19:22
  • @user2908228 If you include you can define `char fname[]` and assign the content of `ep->d_name` to `fname` with `strcpy(fname, ep->d_name)` in the `while` loop. In this case, fname will be overwritten every time, but if you want to process the files one by one (e.g. parsing the exentsion) it should be fine. – PhillipD Oct 24 '13 at 11:51
  • Right okay, thanks. Would you be able to show me that code? If not its fine. – user2908228 Oct 24 '13 at 15:23
0

I asked this question on another forum and got a great answer almost straight away.

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

int main (void)
{
    DIR *dp;
    struct dirent *ep;     
    dp = opendir ("./");
    const int MAXFILES = 100;
    char list[MAXFILES][256];
    int c = 0;

if (dp != NULL)
    {
        while ((ep = readdir (dp)) && (c < MAXFILES)){
            strcpy(list[c],ep->d_name);
            ++c;
        }

(void) closedir (dp);
    }
    else
        perror ("Couldn't open the directory");

return 0;
}