Lillylionhert 0 Newbie Poster

please disregard this as i have figured out what was going on. i will delete this as soon as i know how.

Dani AI

Generated

— good to hear you worked it out. For anyone else who lands here: the surprising character changes people see when using strtok usually come from how strtok works, not from magic.

strtok tokenizes in-place. Each delimiter it finds is overwritten with a NUL ('\0') so the token becomes a C string. That modification is deliberate. Also, calling strtok on a string literal yields undefined behavior because string literals are not mutable — always use a mutable buffer or a copy (for example, from strdup and free it later).

A safe pattern — copy the input, then tokenize and convert the numeric token with strtol:

char src[] = "label:1234";
char buf[sizeof src];
strcpy(buf, src);           /* keep original intact */

char *tok = strtok(buf, ":");
char *numstr = strtok(NULL, ":");
if (numstr) {
    long val = strtol(numstr, NULL, 10);
    printf("label=%s number=%ld\n", tok, val);
}

If you only need to split on the last colon and parse the number, strrchr + strtol is simpler and avoids strtok state issues:

char s[] = "label:1234";
char *p = strrchr(s, ':');
long val = 0;
if (p) {
    val = strtol(p + 1, NULL, 10);
    *p = '\0';    /* optional: split in-place */
}
printf("label=%s number=%ld\n", s, val);

Troubleshooting checklist: ensure the source is mutable; verify the delimiter string passed to strtok; copy tokens into appropriately sized buffers (watch overflows); consider strtok_r (reentrant) or strsep if you need safer reentrancy or nested parsing. See the manual for details: strtok(3) and strtok_r(3).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.