I need to modify a name of file passing by argument with adding "_out" and changing extension. So, first, I copy the name of old file without extension, like that
char* arg1 = argv[1];
char* var1 = NULL;
var1 = malloc(strlen(arg1) * sizeof(char));
strcpy( var1, arg1 );
var1[strlen(var1) - 1] = 'l';
var1[strlen(var1) - 2] = 'm';
var1[strlen(var1) - 3] = 'x';
char* var1Out = NULL;
var1Out = malloc((strlen(var1) + 4) * sizeof(char));
strncpy( var1Out, var1, strlen(var1) - 4 ); //Marker
.
.
.
But when I display var1Out with printf just after "//Marker", by passing "test.txt" by argument, I get : "test└" and finally my program return "test└_out.xml" at the end. While when I modify marker line like this:
strncpy( var1Out, var1, strlen(var1) - 3 ); //Marker
it displays:
test.
and with:
strncpy( var1Out, var1, strlen(var1) - 5 ); //Marker
it displays:
tes
so it works perfectly.
Why does it adds a character when I use:
strncpy( var1Out, var1, strlen(var1) - 4 ); //Marker
?