0

I'm trying to write a function to go through a folder and all it's subfolders to find all "stat" files and print the first two (by space separated) values. The first value is an integer, this is not problematic. The second value is a string and printing the value is causing me some trouble. I'm using strtok() to get the first two values of each file and i've read about what the function does, so the extracting of the information is working correctly. However when i go to print both the array of integers and the array of strings, the strings arent at all what was stored in the first place.

The variables in question are defined like so:

int pidsId = 0;
char delimiter[] = " ";
int* pids = malloc(sizeof(int) * 50);
char** names = malloc(sizeof(char*) * 20);
char* name = malloc(sizeof(char) * 20);
for(int i = 0; i < 20; i++) {
    names[i] = malloc(sizeof(char) * 20);
}

The while loop in which i store the values from files:

while((sde = readdir(subdir)) != NULL) {
    if((sde->d_type == DT_REG) && !strcmp(sde->d_name, "stat")) {
        
        char* statline = malloc(sizeof(char) * 10);
        size_t statsize = 10;
        char* ime = malloc(strlen(subpath) + strlen(sde->d_name) + 2);

        strcpy(ime, subpath);
        strcat(ime, "/");
        statFile = fopen(strcat(ime, sde->d_name), "r");
        
        getline(&statline, &statsize, statFile);
        pids[pidsId] = atoi(strtok(statline, delimiter));
        name = strtok(NULL, delimiter);
        printf("%s\n", name);
        names[pidsId] = name;
                        
        printf("%s\n", names[pidsId]);
        pidsId++;
        free(statline);

        free(ime);

        fclose(statFile);
   }    
}

and finally the problem lines:

for ( int i = 0; i < pidsId; i++) {
    printf("%d %s\n", pids[i], names[i]);
}

To elaborate a bit, when i save the string the print is fine but after i store the name into the names array and the while loop ends something happens to the string when i try to print it. It was to my understanding that printf("%s", char* array) prints out all the characters until '\0' is met. And i've tried printing a simple string array with the same method as in the above code (copy pasted array definitions and print loop) and it printed fine.

I've tried looking around on the internet and haven't found a solution yet. I tried changing my array definition as char names[][] and char* names[] or even tried printing the strings character by character. I've mostly been looking for problems with printing string arrays but nothing has worked so far. It might be due to me looking at the code for so long but i don't see what im doing wrong. What am i missing here? Below is an example of the wanted output (without the print line within the while loop):

16 (dash)
15 (dash)
20 (copyproc.sh)
12 (sleep)
10 (bash)
1 (bash)
14 (dash)
11 (sleep)

I am instead getting this (without the print line within the while loop):

16 
15 ��U
20 
12 
10 ��U
1 ��U
14 ��U
11 ��U

Any and all help with understanding my problem is appreciated.

0

1 Answer 1

2

In your code 'name' points to a part of the stateline. This is guaranteed by the strtok. However, you do free(stateline) in the function, invalidating the name as well.

I suggest you copy the 'name' before freeing the 'stateline', for example using strdup:

 names[pidsId] = strdup(name);
Sign up to request clarification or add additional context in comments.

1 Comment

I see, I didn't think it worked like that, but that's very interesting. Anyways it fixed my problem thank you kind stranger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.