Kastoli Kastoli - 1 month ago 8
C Question

How do I duplicate the string stored in a strtok() token?

I'm trying to duplicate the value of a strtok() token.

I'm running strtok() on a string with volatile value.

char buffer[100];
char temp[100];
for(int i = 0; i < 100; i++){
fgets(buffer, 100, my_file);
fscanf(my_file, "%s", temp)
}


An example value for temp in this case might be "100,200,300"

char my_array[3][100];
char* token;
token = strtok(temp,",");
while(token != NULL){
switch (token[0]){
case '1' :
strcpy(my_array[0], token);
break;
case '2' :
strcpy(my_array[1], token);
break;
case '3' :
strcpy(my_array[2], token);
break;
}
token = strtok(NULL,",");
}


What i'm attempting to do with strcpy() is to duplicate the value of the token, and store the duplicate value in an array.

Answer

Converting your code into an MCVE (Minimal, Complete, Verifiable Example), I get:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char temp[] = "100,200,300";
    char my_array[3][100];
    char *token = strtok(temp, ",");

    while (token != NULL)
    {
        switch (token[0])
        {
        case '1':
            strcpy(my_array[0], token);
            break;
        case '2':
            strcpy(my_array[1], token);
            break;
        case '3':
            strcpy(my_array[2], token);
            break;
        }
        token = strtok(NULL, ",");
    }
    for (int i = 0; i < 3; i++)
        printf("%d: [%s]\n", i, my_array[i]);
    return 0;
}

The output from running that is:

0: [100]
1: [200]
2: [300]

It is not clear what your problem with your code is.

This is clearly not a general solution. It relies on the three tokens starting with the three digits 1, 2, 3 — and on there being exactly 3 tokens, and none of the tokens being longer than 99 characters, and so on. But your code safely copies the tokens into your array.

This is more general code, using the POSIX (but not C) standard function strdup().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char temp[] = "100,200,300,19231,Exploratorium,Extraneous material,91"
                  "9293,and one rather long token without much significance"
                  " except to point out that not all strings are as short as"
                  " 100 bytes long";
    char *my_array[100];
    int count = 0;

    char *token = strtok(temp, ",");
    while (token != NULL)
    {
        if (count >= 100)
            break;
        my_array[count++] = strdup(token);
        token = strtok(NULL, ",");
    }

    for (int i = 0; i < count; i++)
        printf("%d: [%zu][%s]\n", i, strlen(my_array[i]), my_array[i]);

    for (int i = 0; i < count; i++)
        free(my_array[i]);
    return 0;
}

This produces the output:

0: [3][100]
1: [3][200]
2: [3][300]
3: [5][19231]
4: [13][Exploratorium]
5: [19][Extraneous material]
6: [6][919293]
7: [123][and one rather long token without much significance except to point out that not all strings are as short as 100 bytes long]