473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

tokens

1
what are tokens and what does strtok do?
Sep 15 '07 #1
3 1070
JosAH
11,448 Expert 8TB
what are tokens and what does strtok do?
I'll move your question over to the C/C++ Forum section where it belongs.

kind regards,

Jos
Sep 15 '07 #2
Studlyami
464 Expert 256MB
strtok tells how you want to separate a string and where you want to store it.
Heres a basic example.
Expand|Select|Wrap|Line Numbers
  1.     FILE *f_ptr;  //points to an open text file
  2.     f_ptr = fopen("MyText.txt", "rt"); //opens the text file
  3.  
  4.     if(f_ptr == NULL) //error opening file
  5.         return;
  6.  
  7.     char * c_Ptr = NULL; // holds a temparay selection sections
  8.     char c_Ptr1[1000] = {NULL}; //holds the entire line of text
  9.  
  10.     for(;fgets(c_Ptr1, 1000, f_ptr)!=NULL;) //grabs the next line of text
  11.     {
  12.         c_Ptr = strtok(c_Ptr1, ","); // first time to separate hold the location in the line
  13.         while (c_Ptr != NULL)
  14.         c_Ptr = strtok(NULL, ","); 
  15. }
  16.  
  17.  
if the first line of text said "this is, the line of, text separated by a specific token"

the first time strtok c_Ptr = this is
2nd strtok c_Ptr = the line of
3rd strtok c_Ptr = text separated by a specific token

Then when it hits the for loop it grabs the next line in the text file until it find the end of file.
Sep 15 '07 #3
Studlyami
464 Expert 256MB
Wow my example is pretty ugly here is a much cleaner example fro www.cplusplus.com

Expand|Select|Wrap|Line Numbers
  1. /* strtok example */
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main ()
  6. {
  7.   char str[] ="- This, a sample string.";
  8.   char * pch;
  9.   printf ("Splitting string \"%s\" into tokens:\n",str);
  10.   pch = strtok (str," ,.-");
  11.   while (pch != NULL)
  12.   {
  13.     printf ("%s\n",pch);
  14.     pch = strtok (NULL, " ,.-");
  15.   }
  16.   return 0;
  17. }
  18.  
Output:


Splitting string "- This, a sample string." into tokens:
This
a
sample
string
Sep 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jonathan Fine | last post by:
I'm sort of wishing to convert TeX tokens into characters. We can assume the standard (i.e. plain) category codes. And that the characters are to be written to a file. This proceess to take...
2
by: Aurelien Mazurie | last post by:
Hello to all, I'm trying to do a weird (?) thing with regexp and MySQL. For example, i have a column with: token1|token2|token3 (three tokens separated by pipes) I'm trying to write an SQL...
10
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize for not finding it) I have a C-style string that I'd like to cleanly separate into tokens (based on the '.' character) and then convert those tokens to unsigned...
6
by: Bill Cunningham | last post by:
Lexical generators such as Bison, Flex, Lex, &c produce C tokens for the parser or compiler. What do these C tokens look like? According to ANSI C, what does the standard have to say about C...
7
by: Steven Woody | last post by:
in C, is there any function can be used to decompose tokens from a string? if not, can i find it in CPP? thanks! -- steven woody (id: narke) Celine: Well, who says relationships have to last...
4
by: Jay Nesbitt | last post by:
Is there a quick way to extract tokens from a string of text? I know Java provides a StringTokenizer class to do this. Is there something similar in the dot net framework? Ex. string s = "some...
0
by: kamal9 | last post by:
#include <stdio.h> #include <string.h> void string2Lines(char line, char tokens){ char *token_ptr, token; char *i=";" " " "," "." "?" "!"; token_ptr = strtok(line, i ); while(token_ptr){...
1
by: kara18 | last post by:
Hi, After splitting a string in to tokens using strtok ,how can I get the tokens in to different arrays. for example char str = "now # is the time for all # good men to come to the # aid of...
5
by: gpaps87 | last post by:
hi, i wanted to know whether we can use strtok command to mark delimiters as tokens as well.In Java,we have a command: StringTokennizer(String str, String delimiters, boolean delimAsToken) ...
14
by: mdh | last post by:
Hi all, From p 125, gives rise to this issue for me. Is it true that a "token" in C ( philisophically ) is the least amount of digits/chars/underscores/*s ( and other non blank space that I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.