0

I have tokenized a buffer that I had, and at the point of the question, the token holds the following:

token = "Host: localhost:8080"

I'm trying to break apart each word so that I can check the HTTP request for "Content-Length: ", which will come up if I parse the next line. I am attempting to do this with another token, but every time I do the following, it overwrites the local token variable:

poken = strtok(token, " ");

poken prior to the execution of it

spoken after it's execution

Is there another way to isolate strings using spaces as a delimiter?

Rob
  • 14,746
  • 28
  • 47
  • 65
  • 1
    Does this answer your question? [strtok when process two strings at same time](https://stackoverflow.com/questions/33465800/strtok-when-process-two-strings-at-same-time) - short answer No you can't do that. – EvilTeach May 19 '20 at 20:00
  • 1
    `strtok` is not re-entrant or thread-safe. You must extract all the token pointers first, before processing them. – Weather Vane May 19 '20 at 20:04
  • 2
    **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob May 19 '20 at 20:05

1 Answers1

0

Consider using strchr to locate delimiters, and strstr to locate words in your string. strtok looks good but is generally not a good tool for parsing things. When doing CSV files in particular for "x,y,,z" one would expect to get 4 fields back. You only get 3, as strtok will silently swallow the ",,".

EvilTeach
  • 28,120
  • 21
  • 85
  • 141