I’m working with C and through a socket I will be receiving a message with one space in it, I need to split the string into parts at the space. How would I go about doing this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
strtok_r is your friend.
Don’t use plain
strtok(), as it’s NOT thread-safe.Even on platforms where it is thread-safe (because the state is held in Thread-Local Storage), there is still the problem that the use of internal state means you cannot parse tokens from several strings simultaneously.
for example, if you write a function which uses
strtok()to separate string A, your function cannot be called within the loop of a second function which is usingstrtok()to split string B.