James Guvna Redmond
James Guvna Redmond

Reputation: 183

Concat multiple strings?

I have a line like the one below, but I need to concat slashes for directories, is there any way to safely concat multiple strings?

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

Upvotes: 0

Views: 454

Answers (2)

pmg
pmg

Reputation: 108978

Supposing there is enough space all around, this works

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

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318568

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

Upvotes: 5

Related Questions