1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

strlcat problem: why strlcat does not work?

Discussion in 'C++' started by dbigbear, Sep 15, 2011.

  1. dbigbear

    dbigbear New Member

    Joined:
    Sep 15, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Sep 16, 2011
  2. dbigbear

    dbigbear New Member

    Joined:
    Sep 15, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page