Defining a path using #define in C

I want to define a path like this:

#define PATH /abc/xyz/lmn

      

This PATH is the directory that contains the files foo1, foo2, foo3, ... foo115.

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

I want to basically do this with a directive:

fd = open("/abc/xyz/lmn/foo1", O_RDONLY);

      

+3


source to share


3 answers


#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);
   }

}

      



+9


source


#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 with some C ++, sorry. I tried to keep it as C as I could.

+1


source


For example, to open foo42

, you can do:

#define PATH  "/abc/xyz/lmn"
fd = open(PATH "/foo42", O_RDONLY);

      

0


source







All Articles