Concat多个字符串?

时间:2011-03-10 23:05:02

标签: c

我有一个如下所示的行,但是我需要为目录连接斜杠,有没有办法安全地连接多个字符串?

// Need to include \\ after windowsDir
FILE *dest = fopen(strcat(windowsDir, filename),"wb");

2 个答案:

答案 0 :(得分:5)

char *buf = malloc(strlen(windowsDir) + 1 + strlen(filename) + 1); // len + \ + len + \0
sprintf(buf, "%s\\%s", windowsDir, filename);
FILE *dest = fopen(buf, "wb");
free(buf);

答案 1 :(得分:0)

假设周围有足够的空间,这可行

strcpy(buff, "C:\\foobar");
strcpy(value, "file.txt");
strcat(strcat(buff, "\\"), value);
/* buff now has "C:\\foobar\\file.txt" */