How can I handle this string concatenation in C in a reusable way

Posted by hyphen this on Programmers See other posts from Programmers or by hyphen this
Published on 2014-03-26T17:58:44Z Indexed on 2014/06/02 15:57 UTC
Read the original article Hit count: 181

Filed under:
|
|

I've been writing a small C application that operates on files, and I've found that I have been copy+pasting this code around my functions:

char fullpath[PATH_MAX];
fullpath[0] = '\0';
strcat(fullpath, BASE_PATH);
strcat(fullpath, subdir);
strcat(fullpath, "/");
strcat(fullpath, filename);

// do something with fullpath...

Is there a better way? The first thought that comes to mind is to create a macro but I'm sure this is a common problem in C, and I'm wondering how others have solve it.

© Programmers or respective owner

Related posts about c

    Related posts about strings