LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2011, 04:31 PM   #1
hajiman
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Rep: Reputation: Disabled
strtok behavior when character delimiter is the first char in string


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.

Example:
char temp_str[10];
temp_str[0] = '\n';
temp_str[1] = '\0';

strtok (temp_str, "\n");

if (temp_str[0] == '\n')
printf("newline still there\n");

if tmp_str[0] == '\n' before a call to strtok, tmp_str[0] == '\n' after the call.

Thanks!
 
Old 08-03-2011, 07:14 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by hajiman View Post
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.

Last edited by dwhitney67; 08-03-2011 at 07:15 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
change for-loop delimiter character? galle Programming 6 08-19-2009 10:15 AM
How use CUT with several chars delimiter (not a single char) ? frenchn00b Programming 6 04-22-2009 10:11 PM
Need 'cut' with mulit char delimiter endfx Programming 5 03-04-2009 01:37 AM
how to print the first character in a string using strtok its_godzilla Programming 5 02-02-2005 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration