0

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:

cobb208
  • 55
  • 1
  • 7
  • 3
    `strcat(pkg_name, &c);` That is wrong because the `strcat` requires both params to be strings. A single character is not a string and the result is Undefined Behaviour. – kaylum Nov 21 '21 at 03:50
  • @kaylum thank you, I changed around some things and using a different method fixed it, appreciate the feedback! – cobb208 Nov 21 '21 at 23:45

1 Answers1

0

Well thank you to kaylum and this article: How to get char* with unknown length in C?

I was able to solve my issue by removing the function and using realloc inside the while loop please see below and I hope it helps someone else out in the future!

The pkg_name & pkg_install_dir are still using a char buffer because I realized using the realloc way from the article the two points of memory were rolling over each other (good amount of time to figure that out and I will never get back!).

Learning C is fun!

int read_db()
{
FILE *db;

db = fopen("/tmp/devpkg/db/db", "r");

char *pkg_name = malloc(MAX_CHAR_BUFF);
char *pkg_install_dir = malloc(MAX_CHAR_BUFF);
int is_pkg_name = 1;
int actual_file_size = 0;
while(1)
{

    char c = fgetc(db);
    if(c == EOF)
    {
        pkg_install_dir = realloc(pkg_install_dir, (actual_file_size - 1));
        pkg_install_dir[actual_file_size - 1] = '\0';
        break; // while loop will finish here
    }

    if(is_pkg_name) {
        pkg_name[actual_file_size] = c;
    } else {
        pkg_install_dir[actual_file_size] = c;
    }

    actual_file_size++;
    
    if(c == ',')
    {
        pkg_name = realloc(pkg_name, (actual_file_size - 1));
        pkg_name[actual_file_size - 1] = '\0';

        is_pkg_name = 0;
        actual_file_size = 0;
        printf("The string is:%s\n", pkg_name);
    }

    
    
}

fclose(db);

free(pkg_name);
free(pkg_install_dir);

return 0;

}

cobb208
  • 55
  • 1
  • 7