I, I come across a strange problem when using strlcat, as follows: Code: static int get_path(char *parent, char * file){ int parent_len = strlen(parent); int file_len = strlen(file); if(parent_len + file_len > 256) { return 0; } int ret = strlcat(parent, file, file_len); } int main(int argc, char *argv[]) { char path[256]; char file[256]; snprintf(path, 256, "%s", "./parent/"); snprintf(file, 256, "%s", "test.dat"); get_path(path, file); cout << path << endl; cout.flush(); } The output is : ./parent/ The problem is why the output is "./parent/"? Is it supposed to be "./parent/test.dat"? Thanks Xiong
I would like make some complement about the question. strlcat works when I put all in main instead of using an extra fuction. That is: main...{ char path[256] char file1[256] snprintf(path, 256, "%s", "./test/"); snprintf(file1, 256, "%s", "testtt.dat"); int file1_len = strlen(file1); strlcat(path, file1, file1_len); cout << path << endl; } The output is ./test/testtt.dat... SO why this works but previous one??? Thanks Xiong
How about posting the ACTUAL code? Difficult to comment on the behaviour of code we can't see. Actually the output of Code: main...{ char path[256] char file1[256] will be a stack of syntax errors because this code is invalid. After RTFM-ing on strlcat again, have a close look at the line Code: int file_len = strlen(file); and have a think about what effect the third parameter will have on the line Code: strlcat(parent, file, file_len); Incidentally, if you think that Code: char str[256]; sprintf(str,"hello"); printf("%d",strlen(str)); will display 256, try running the code and see what you ACTUALLY get. Hint: it won't be 256, unless your implementation of strlen is broken. If your second code sample "works" then I would suggest your implementation of strlcat is broken. strlen("testtt.dat") should be 10, so path should contain "./test/te" (9 characters visible, plus 1 for the terminating NULL) after the strlcat.