在C中使用#define定义路径

Vin*_*Vin 3 c linux c-preprocessor

我想定义一个这样的路径:

#define PATH /abc/xyz/lmn
Run Code Online (Sandbox Code Playgroud)

该PATH是具有文件foo1,foo2,foo3,... foo115的目录.

如何在"打开"调用中使用此#define打开foo1,foo2,... foo115?

我想基本上使用指令执行此操作:

fd = open("/abc/xyz/lmn/foo1", O_RDONLY);
Run Code Online (Sandbox Code Playgroud)

Kev*_*imm 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);
   }

}
Run Code Online (Sandbox Code Playgroud)

  • 既然你还在使用sprintf,那么const char*不会比定义好吗? (2认同)