Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   strlcat problem: why strlcat does not work? (http://www.go4expert.com/forums/strlcat-strlcat-t26711/)

dbigbear 15Sep2011 20:44

strlcat problem: why strlcat does not work?
 
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

dbigbear 15Sep2011 20:47

Re: strlcat problem: why strlcat does not work?
 
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

Quote:

Originally Posted by dbigbear (Post 86999)
I,

I come across a strange problem when using strlcat, as follows:

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


xpi0t0s 16Sep2011 04:27

Re: strlcat problem: why strlcat does not work?
 
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.


All times are GMT +5.5. The time now is 05:34.