我想要定义一个这样的path:
#define PATH /abc/xyz/lmn
这个PATH是一个包含文件foo1,foo2,foo3,… foo115的目录。
如何在“打开”调用中使用此#define打开foo1,foo2,… foo115?
我想基本上这样做使用该指令:
fd = open("/abc/xyz/lmn/foo1", O_RDONLY);
#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); } }
#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 }
我可能混了一些C ++,对不起。 我试图把它保持为C。
例如,要打开foo42
你可以这样做:
#define PATH "/abc/xyz/lmn" fd = open(PATH "/foo42", O_RDONLY);