I have a function that is reading from a file for package names and their directories. I have setup a variable to catch one item before the other (they are separated by a comma). When I append to my string it seems to add it just fine. However, when I go to realloc the string (compress it down to its actual size) it owns retains half of the characters.
When I used to a for loop to test it, there are empty characters being projected between each character. So I have a test work "duck" when I print it as a string it will write "duck" however if it iterate over it, it will go "d" " " "u" " " "c"
while(1)
{
char c = fgetc(db);
actual_file_size++;
if(c == EOF)
{
//pkg_install_dir = realloc(pkg_install_dir, (sizeof(pkg_install_dir) + 2));
//printf("The install dir is: %s\n", pkg_install_dir);
break;
}
if(is_pkg_name) {
printf("Chracter being added to pkg_name: %c\n", c);
strcat(pkg_name, &c);
} else {
strcat(pkg_install_dir, &c);
}
if(c == ',')
{
printf("Actual file size int is: %d\n", actual_file_size);
printf("Package name before realloc: %s\n", pkg_name);
for(int i = 0; i < 5; i++)
{
printf("%c\n", pkg_name[i]);
}
pkg_name = realloc(pkg_name, actual_file_size);
is_pkg_name = 0;
actual_file_size = 0;
printf("The string is:%s\n", pkg_name);
}
Terminal output: