LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-13-2004, 02:21 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
question about strtok_r built-in function in C


question about strtok_r built-in function in C

If a string has many words in it then, strtok_r will not work correctly.
Note: A word within a string is separated by a blank space.
s_1 output is incorrect. s_1 should be 10.4.0.1

If a string has only one word in it, then strtok_r will work correctly.
s_2 output is correct. s_2 output is 10.4.0.1

Is there a function that will do what strtok_r except that it will
search for a long string that contains many words ? Using a built-in
function to search for a word or a character pattern within a very long
string saves time because I don 't have to use a loop with an if-then-else
statement.

==================================

root:/home# ./string_1
using strtok_r function. s_1 = bbb
using strtok_r function. s_2 = 10.4.0.1

==================================
Code:
#include <stdio.h>       
#include <stdlib.h>       
#include <string.h>

main ()

{
  char ip_address_string_1[30] = "aaa bbb ddd addr:10.4.0.1";
  char ip_address_string_2[30] = "addr:10.4.0.1";

  char *s_1;
  char *s_2;
  char *save_pointer;
  char *ip_address;

  s_1 = strtok_r(ip_address_string_1, "addr:",  &save_pointer);
  printf ("using strtok_r function.  s_1 = %s\n", s_1);

  s_2 = strtok_r(ip_address_string_2, "addr:",  &save_pointer);
  printf ("using strtok_r function.  s_2 = %s\n", s_2);
}
 
Old 05-13-2004, 03:05 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I'm not sure you're using strtok_r correctly. You want to do something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TOKENS "."

int
main (int argc, char *argv[])
{
static char ip_addr[] = "10.4.0.1";
char *buf_pp = NULL;
char *s = NULL;
int count = 0;

/* Parse the first argument */
s = strtok_r (ip_addr, TOKENS, &buf_pp);

while (s) {
/* Increment count */
count++;

/* Print the current token */
printf ("token[%d]: 0x%02x\n",
count, atoi (s));

/* Get the next token until end of string */
s = strtok_r (NULL, TOKENS, &buf_pp);
}

/* Done */
return printf ("Done.\n");
}

You should see something like this:
token[1]: 0x0a
token[2]: 0x04
token[3]: 0x00
token[4]: 0x01


The key points are:
1. Call strtok_r() the first time *with* your target string; call it in a loop
to get the remaining tokens, call strtok_4 all subsequent times with a *NULL*
instead of your target string.

2. The delimiters string holds a list of *one character delimiters*.
In other words, saying "addr:" actually means "delimit on 'a' or on 'b' or on 'c' ...

3. You'd want one "save_pointer" for parsing all the tokens in one string, and
a *different* buffer pointer variable for parsing tokens from a different string.

Hope that helps .. PSM
 
Old 05-13-2004, 04:43 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Thank you paulsm4 for your help.
 
Old 05-13-2004, 08:41 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can do what you're looking for strstr().

Code:
s_1 = strstr(ip_address_string_1, "addr:")+5;

printf("IP address is: %s\n", s_1);
You'll probably want to check to make sure strstr() doesn't return NULL and such first if it's not "trusted input".
 
Old 05-14-2004, 10:33 AM   #5
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

thank you itsme86 for your assistance.
 
  


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
warning: incompatible implicit declaration of built-in function ‘exit’ xzotech Programming 1 08-14-2005 05:43 AM
built-to-order PC or ready-built PC from well-known companies? kpachopoulos General 6 11-03-2004 03:14 PM
C built-in function for a Bash script Linh Programming 3 04-23-2004 09:23 AM
software management question (rpms and self-built) steyr Linux - General 1 12-30-2003 10:25 AM
if i get an nforce mobo with built-in VGA... will the built in vga work in linux? kublador Linux - Hardware 2 11-10-2003 08:26 PM

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

All times are GMT -5. The time now is 07:08 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