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

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] = '';
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.

7

Reusable for a function like the one you describe means several things:

  • Doesn’t assume the input is valid.
  • Uses sane defaults.
  • Prevents buffer overruns.
  • Returns some indication of success or failure.

In your case:

#define BASE_PATH "/path/to/wherever"
bool build_path(char *dest, size_t size, char *subdir, char *filename)
{
    // Don't assume the input is valid
    if ( (dest == NULL) || (size < 1) || (filename == NULL) ) {
        return false;
    }

    // Make no subdir work  (sane default behavior)
    if ( subdir == NULL ) {
        subdir = "";
    }

    // Prevent buffer overruns by using a safe formatting function
    size_t stored = snprintf(dest, size, BASE_PATH "/%s/%s", subdir, filename);

    // Returns success (true) if the path fit the buffer
    return ((stored+1) <= size);
}


char fullpath[PATH_MAX];
if ( ! build_path(fullpath, sizeof(fullpath), "foo/bar", "baz") ) {
   // Handle error
}

1

How about passing the address of fullpath, subdir, and filename into a function and have that function concatenate all of the strings for you?

Something to the effect of:

void main(...)
{
    char fullpath[PATH_MAX];
    memset(fullpath, 0x00, PATH_MAX); //note: probably wrong syntax for memset
    ...
    ConcatPath(&fullpath, subdir, filename);
}

ConcatPath(char *fullpath, char *subdir, char *filename)
{
    strcat(fullpath, BASE_PATH);  //note need some casting magic here to reference
    strcat(fullpath, subdir);     // fullpath & other strings correctly.
    strcat(fullpath, "/");
    strcat(fullpath, filename);
}

6

In my opinion, as a professional programmer, you must not write code like that. You must either write your own path concatenation function, or use the one provided by the platform.

If it’s Windows you should use the _makepath function. For greater safety, use the _makepath_s function. It will be more careful than you are. I’m sure Posix has something similar.

If you must write your own, simple concatenation is just not good enough. You need to check for proper handling of delimiters, trailing slash on directories, leading period on extensions, null arguments and all that stuff. The good news is that if you do it once it’s done.

In the more general case of concatenating strings, you are very hampered by your apparent decision to stick with C rather than C++. I would again recommend writing your own function with variable arguments (vararg) and using strcat_s() for security. Something like:

bool strcat_multi_s(char *destination, size_t maxlength, ...); // return false on overflow

I leave the coding as an exercise for the reader.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

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

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] = '';
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.

7

Reusable for a function like the one you describe means several things:

  • Doesn’t assume the input is valid.
  • Uses sane defaults.
  • Prevents buffer overruns.
  • Returns some indication of success or failure.

In your case:

#define BASE_PATH "/path/to/wherever"
bool build_path(char *dest, size_t size, char *subdir, char *filename)
{
    // Don't assume the input is valid
    if ( (dest == NULL) || (size < 1) || (filename == NULL) ) {
        return false;
    }

    // Make no subdir work  (sane default behavior)
    if ( subdir == NULL ) {
        subdir = "";
    }

    // Prevent buffer overruns by using a safe formatting function
    size_t stored = snprintf(dest, size, BASE_PATH "/%s/%s", subdir, filename);

    // Returns success (true) if the path fit the buffer
    return ((stored+1) <= size);
}


char fullpath[PATH_MAX];
if ( ! build_path(fullpath, sizeof(fullpath), "foo/bar", "baz") ) {
   // Handle error
}

1

How about passing the address of fullpath, subdir, and filename into a function and have that function concatenate all of the strings for you?

Something to the effect of:

void main(...)
{
    char fullpath[PATH_MAX];
    memset(fullpath, 0x00, PATH_MAX); //note: probably wrong syntax for memset
    ...
    ConcatPath(&fullpath, subdir, filename);
}

ConcatPath(char *fullpath, char *subdir, char *filename)
{
    strcat(fullpath, BASE_PATH);  //note need some casting magic here to reference
    strcat(fullpath, subdir);     // fullpath & other strings correctly.
    strcat(fullpath, "/");
    strcat(fullpath, filename);
}

6

In my opinion, as a professional programmer, you must not write code like that. You must either write your own path concatenation function, or use the one provided by the platform.

If it’s Windows you should use the _makepath function. For greater safety, use the _makepath_s function. It will be more careful than you are. I’m sure Posix has something similar.

If you must write your own, simple concatenation is just not good enough. You need to check for proper handling of delimiters, trailing slash on directories, leading period on extensions, null arguments and all that stuff. The good news is that if you do it once it’s done.

In the more general case of concatenating strings, you are very hampered by your apparent decision to stick with C rather than C++. I would again recommend writing your own function with variable arguments (vararg) and using strcat_s() for security. Something like:

bool strcat_multi_s(char *destination, size_t maxlength, ...); // return false on overflow

I leave the coding as an exercise for the reader.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

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

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] = '';
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.

7

Reusable for a function like the one you describe means several things:

  • Doesn’t assume the input is valid.
  • Uses sane defaults.
  • Prevents buffer overruns.
  • Returns some indication of success or failure.

In your case:

#define BASE_PATH "/path/to/wherever"
bool build_path(char *dest, size_t size, char *subdir, char *filename)
{
    // Don't assume the input is valid
    if ( (dest == NULL) || (size < 1) || (filename == NULL) ) {
        return false;
    }

    // Make no subdir work  (sane default behavior)
    if ( subdir == NULL ) {
        subdir = "";
    }

    // Prevent buffer overruns by using a safe formatting function
    size_t stored = snprintf(dest, size, BASE_PATH "/%s/%s", subdir, filename);

    // Returns success (true) if the path fit the buffer
    return ((stored+1) <= size);
}


char fullpath[PATH_MAX];
if ( ! build_path(fullpath, sizeof(fullpath), "foo/bar", "baz") ) {
   // Handle error
}

1

How about passing the address of fullpath, subdir, and filename into a function and have that function concatenate all of the strings for you?

Something to the effect of:

void main(...)
{
    char fullpath[PATH_MAX];
    memset(fullpath, 0x00, PATH_MAX); //note: probably wrong syntax for memset
    ...
    ConcatPath(&fullpath, subdir, filename);
}

ConcatPath(char *fullpath, char *subdir, char *filename)
{
    strcat(fullpath, BASE_PATH);  //note need some casting magic here to reference
    strcat(fullpath, subdir);     // fullpath & other strings correctly.
    strcat(fullpath, "/");
    strcat(fullpath, filename);
}

6

In my opinion, as a professional programmer, you must not write code like that. You must either write your own path concatenation function, or use the one provided by the platform.

If it’s Windows you should use the _makepath function. For greater safety, use the _makepath_s function. It will be more careful than you are. I’m sure Posix has something similar.

If you must write your own, simple concatenation is just not good enough. You need to check for proper handling of delimiters, trailing slash on directories, leading period on extensions, null arguments and all that stuff. The good news is that if you do it once it’s done.

In the more general case of concatenating strings, you are very hampered by your apparent decision to stick with C rather than C++. I would again recommend writing your own function with variable arguments (vararg) and using strcat_s() for security. Something like:

bool strcat_multi_s(char *destination, size_t maxlength, ...); // return false on overflow

I leave the coding as an exercise for the reader.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật