concat problems

Having some problems copying one string to other. I copied strings in my other functions and they work fine but when I try the same thing I get garbage in my string. I do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        void modified_recursion(char *base_dir, char *modified_dir, 
                                char *base_name, char *modified_name)
        {
	    int length;
            //stings for sub-directories
	    char *temp_base = malloc(MAX_DIR_LENGTH);
	    char *temp_modified = malloc(MAX_DIR_LENGTH);

	    length = strlen(base_dir);
	    strncpy(temp_base, base_dir, length);
            length = strlen(modified_dir);
	    strncpy(temp_modified, modified_dir, length);

	    //Create a string for the base sub-directory
	    strncat(temp_base, "/", MAX_DIR_LENGTH);
	    strncat(temp_base, base_name, MAX_DIR_LENGTH);
	    strncat(temp_base, "/", MAX_DIR_LENGTH);

	    //Create a string for the base sub-directory
	    strncat(temp_modified, "/", MAX_DIR_LENGTH);
	    strncat(temp_modified, modified_name, MAX_DIR_LENGTH);
	    strncat(temp_modified, "/", MAX_DIR_LENGTH);

            //process the sub-directories....
        }


I have done this same thing in another function but I do something different when processing the sub-directories but have no problems with it. I have thrown some printf in there and when I create temp_base I get some garbage like y@y@-dir/ then when I copy the base sting to the temp_base string it just adds it to the begaining of the string. Something like dir1y@y@-dir/. This is realy weird I must be doing something wrong here. Thanks for help!
length = strlen( foo ) +1;
You are forgetting the terminating null.
WOW I forgot about that. Thanks!!!!!
Topic archived. No new replies allowed.