I'm coding some program and at one point of my code, I'm browsing a directory. In order to make a string concording to the path and file name, I have this code:
char *aux;
aux = (char *) malloc(strlen(directory) + strlen(files[j] + 1));
sprintf(aux, "%s/%s", directory, files[j]);
//Debugging...
char b[100]; sprintf(b, "Path + file: %s\n", aux); write(1, b, strlen(b));
char c[100]; sprintf(c, "File: %s\n", files[j]); write(1, c, strlen(c));
When I execute my program, the output I get is:
Path + file: ./Path/file.txt!
File: file.txt
As you can see, in first output string, file has an extra character (!) that doesn't belong to the path nor the file (as you can see in the second output string). I also tried adjusting the malloc size to different values and this still happens. Why?