Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining path using #define in C

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);
like image 291
Vin Avatar asked Dec 27 '22 04:12

Vin


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

}
like image 64
KevinDTimm Avatar answered Jan 17 '23 06:01

KevinDTimm


#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.

like image 22
Drise Avatar answered Jan 17 '23 06:01

Drise