How should I approach this problem? r.e. How to use opendir()/scandir()

Hi,

I am currently writing a program but am beginning to see that I could have problems in the future due to the number of files that I may want to open with too many names to specify individually.

I have a series of .txt files. An unspecified number of them. I would like to open each of them in turn and read the data into my program. I am wondering whether I should copy the code each time I want to open a new file, changing the file specific parts or whether there is a more efficient way of going about it?

At the moment the program is only running on a set of test files but in the future I would be looking to expand it and could potentially have an huge number of files that I would be looking to open.

Also, is there a way of automatically detecting the filename and then using that as the name for say a vector or a variable in my program. I think I am going to really need the program to do this for me.

Thanks for your help! You people here are amazing! :-)

Last edited on
You can feed the program a directory upon starting it, and have it read the contents of that directory, make a list of the contents, identify the files of interest by name, and then operate on each of those.

If you can nail down your requirements a bit more clearly, we can point you at helpful functions.
Great idea! That sounds ideal!! I'll have a little look into it :-)

Thanks!
Ok I think I need to use opendir(). I have found this piece of code but it is currently kicking out all kinds of errors. I was just wondering if someone might be so kind as to talk me through it somewhat. Here is what I have;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

void scandir(char *dirname)
{
   DIR *dir;
   struct dirent *ent;

   printf("First pass on '%s':\n",dirname);
   if ((dir = opendir(dirname)) == NULL)
   {
     perror("Unable to open directory");
     exit(1);
   }
   while ((ent = readdir(dir)) != NULL)
     printf("%s\n",ent->d_name);

   printf("Second pass on '%s':\n",dirname);
   rewinddir(dir);
   while ((ent = readdir(dir)) != NULL)
     printf("%s\n",ent->d_name);
   if (closedir(dir) != 0)
     perror("Unable to close directory");
}

void main(int argc,char *argv[])
{
   if (argc != 2)
   {
     printf("usage: opendir dirname\n");
     exit(1);
   }
   scandir(argv[1]);
   exit(0);
}


I am currently replacing all the places where it says dirname with /directory1/directory2/ etc.
Last edited on

Ok I have got the code above to compile now, but what I would like it to be able to do is use it to open first, the first file in a directory. Then I have something I want to do to the data. I then want to move on to the next file and repeat the process.

Am I right in thinking that this code simply open a directory and not much more? I would like to be able to take the name of the first file in the directory and then open that file. Any Ideas how I might be able to achieve this?

Cheers! :-)
currently it's just printing the names on line 17. You could pass an array or better vector where you actually store the names.

I think the code from line 19 until 22 isn't necessary.
Wait this code is only printing on line 31 at the moment. I'll just check it.
Topic archived. No new replies allowed.