Question: I am trying to create a program that takes in a number of processes (name, start time, remaining time) from a file, then uses a round robin algorithm to handle the queue.

The issue is, when I try to tokenize each line of the file by using `strtok()` and `fgets()`, the name of the process is always wrong.

For example, if the first line is `P1 0 3` the output is like this:

[![image](https://i.stack.imgur.com/vpK1k.png)](https://i.stack.imgur.com/vpK1k.png)

```c
void RoundRobin(char *filename) {
    Queue *q = initQueue();
    char string[MAX_SIZE];
    FILE *file;
    Process process[20];
    char *token;
    file = fopen(filename, "r");
    if (!file) {
        printf("File Cannot Be Opened");
    }
    fgets(string, 150, file);
    token = strtok(string, "=");
    token = strtok(NULL, "+");
    int time_quantum = atoi(token);
    int process_count = 0;
    while (fgets(string, 150, file)) {
        char *token1;
        token1 = strtok(string, " ");
        process[process_count].name = token1;

        token1 = strtok(NULL, " ");
        process[process_count].starting_time = atoi(token1);

        token1 = strtok(NULL, " ");
        process[process_count++].remaining_time = atoi(token1);
        token1 = strtok(NULL, " ");
    }

    for (int i = 0; i < process_count; i++) {
        printf("%s  %d  %d\n", process[i].name, process[i].starting_time, process[i].remaining_time);
    }

    fclose(file);
}

```

Answer: 
You are reusing a single `char[]` for all of your token parsing. `fgets()` will overwrite the contents of that `char[]` each time, and `strtok()` will return pointers to memory inside of that `char[]`. Thus, each time you read a new line from the file, the previous pointers you already stored in the `process[]` array are still pointing at the same memory, but the contents of that memory have been altered.

You need to instead allocate a separate `char[]` string for each `name` that you want to save in the `process[]` array. You can use `strdup()` for that, eg:

```c
while (fgets(string, 150, file)){
    char* token1 token1 = strtok(string, " ");
    process[process_count].name = strdup(token1); // <-- HERE

    ...
}

// use process[] as needed...

for(int i = 0; i < process_count; i++){
    free(process[i].name);
}

```
The problem is `strtok()` returns a pointer into the line that it parses. Hence all entries in the `process` array point to the same `string` array that is modified by the call to `fgets()`.

You must duplicate the string you store in the process description structure:

```
void RoundRobin(const char *filename) {
    char string[MAX_SIZE];
    Process process[20];
    Queue *q = initQueue();
    char *token;
    FILE *file;
    file = fopen(filename, "r");
    if (!file) {
        printf("Cannot open file %s\n", filename);
        return;
    }
    int time_quantum = 0;
    int process_count = 0;

    if (fgets(string, sizeof string, file)
    &&  (token = strtok(string, "=")) != NULL
    &&  (token = strtok(NULL, "+")) != NULL) {
        time_quantum = atoi(token);
    }
    while (fgets(string, sizeof string, file)) {
        char *token1;

        if ((token1 = strtok(string, " ")) == NULL)
            contine;
        process[process_count].name = strdup(token1);

        if ((token1 = strtok(string, " ")) == NULL)
            contine;
        process[process_count].starting_time = atoi(token1);

        if ((token1 = strtok(string, " ")) == NULL)
            contine;
        process[process_count].remaining_time = atoi(token1);
        process_count++;
    }

    for (int i = 0; i < process_count; i++) {
        printf("%s  %d  %d\n", process[i].name, process[i].starting_time, process[i].remaining_time);
    }
    for (int i = 0; i < process_count; i++) {
        free(process[i].name);
    }
    fclose(file);
}

```