Quote:
Originally Posted by hajiman
I use strtok to remove the first occurrence of newline character from a string. If newline is the first character in the string strtok does not substitute '\0' for it. Does this seem like the correct behavior? I read the man pages for strtok but didn't see anything that indicates it should behave this way.
|
The man-page does indicate that strtok() returns a pointer to a null-terminated string containing the
next token, and if none are found, then NULL is returned. Bear in mind that when you pass the list of delimiters, you need to note that the null-character is one of those delimiters.
Thus when you pass "\n" as your delimiter, strtok() will parse your string using both '\n' and '\0' as the delimiters. strtok() will skip over delimiter characters that occur at the beginning of the string in search of either a non-delimiter character or the null-character, which delimits the string itself. When the first non-delimiter character is found, strtok() will mark that location, so that it can return it to the caller. Of course, if the string you pass is already pointing at the null-character or consists entirely of your delimiter characters, then strtok() will return NULL.
I realize what I have stated above pretty much summarizes what you have already observed, but hopefully you will rest assured knowing that strtok() does not have a bug.