4

I want to define a path like this:

#define PATH /abc/xyz/lmn

This PATH is a directory which has files foo1, foo2, foo3, ... foo115.

How can I use this #define in the "open" call to open foo1, foo2, ... foo115 ?

I want to basically do this using the directive:

fd = open("/abc/xyz/lmn/foo1", O_RDONLY);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vin
  • 717
  • 1
  • 12
  • 26

3 Answers3

9
#define PATH "/abc/xyz/lmn"

int main (int argc, char **argv)
{
   char file2open[256];
   int i;

   for (i = 1; i <= 115; i++)
   {
      sprintf (file2open, "%sfoo%d", PATH, i);
      fd = open (file2open, O_RDONLY)
      ......
      close (fd);
   }

}
KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
1
#define PATH "/some/path/to/foo/files"

for (int i = 0; 1 < SomeNumberOfFiles; i++)
{
    char carray[256] = strcat(PATH, "foo");
    carray = strcat(carray, char(i));
    //Do something with the carray filename
}

I may have mixed in some C++, sorry. I tried to keep it as C as a I could.

Drise
  • 4,310
  • 5
  • 41
  • 66
0

For example, to open foo42 you could do:

#define PATH  "/abc/xyz/lmn"
fd = open(PATH "/foo42", O_RDONLY);
ouah
  • 142,963
  • 15
  • 272
  • 331