473,320 Members | 1,947 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.

problem with the last string token divided by strtok.

121 100+
Here is my code, just to test the strtok functionality:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct sampleClass
  6. {
  7.    int num;
  8.    char *text;
  9.    struct sampleClass *next;
  10. };
  11.  
  12. typedef struct sampleClass node;
  13.  
  14. void fillinnode(node *target, int num, char **text);
  15. void printoutnodes(node *startnode);
  16.  
  17.  
  18. int main()
  19. {
  20.    char text[30];
  21.    char *token1, *token2, *token3;
  22.    int i;
  23.  
  24.    node *a=(node *) malloc(sizeof(node));
  25.    node *b=(node *) malloc(sizeof(node));
  26.    node *c=(node *) malloc(sizeof(node));
  27.  
  28.    a->next = b;
  29.    b->next = c;
  30.    c->next = NULL;
  31.  
  32.    for(i=0; i<30; i++)
  33.    {
  34.       if (i%10 == 0&&i!=0)
  35.          text[i]=':';
  36.       else text[i]='*';
  37.    }
  38.  
  39.    printf("original string is");
  40.    for(i=0; i<30; i++)
  41.       printf("%c", text[i]);
  42.    printf("\n");
  43.  
  44.    token1 = strtok(text, ":");
  45.    printf("token1=%s\n", token1);
  46.  
  47.    token2 = strtok(NULL, ":");
  48.    printf("token2=%s\n", token2);
  49.  
  50.    token3 = strtok(NULL, "");
  51.    printf("token3=%s\n", token3);
  52.  
  53.    fillinnode(a, 1, &token1);
  54.    fillinnode(b, 2, &token2);
  55.    fillinnode(c, 3, &token3);
  56.  
  57.    printoutnodes(a);
  58. }
  59.  
  60. void fillinnode(node *target, int num, char **text)
  61. {
  62.    target->num = num;
  63.    target->text = *text;
  64. }
  65.  
  66. void printoutnodes(node *startnode)
  67. {
  68.    node *temp = startnode;
  69.    while(temp!= NULL)
  70.    {
  71.       printf("%d", temp->num);
  72.       printf("%s", temp->text);
  73.       temp = temp->next;
  74.    }
  75. }
It compiled well with gcc and runs. However, the last string token is not closed well, see the output:
Expand|Select|Wrap|Line Numbers
  1. charlie$ gcc why.c
  2. charlie$ ./a.out
  3. original string is**********:*********:*********
  4. token1=**********
  5. token2=*********
  6. token3=*********
  7. 1**********2*********3*********��charlie$
  8.  
In my linux terminal, the end of token is displayed with a '(?)' symbol, which probably means this token is not closed well. How to fix this problem?
Oct 16 '07 #1
2 3372
weaknessforcats
9,208 Expert Mod 8TB
Problem 1: Your text string has no null terminator. strtok uses that to tell where your string ends:
for(i=0; i<30; i++)
{
if (i%10 == 0&&i!=0)
text[i]=':';
else text[i]='*';
}
text[i] = '\0'; <<<< I added this.
Problem 2: your text array ends up with 33 characters in it and you allocated only 30. This causes a crash when main() returns. I changed text to an array of 40 and the crash went away.

Clearly, it's time for you to start learning how to use your debugger.
Oct 16 '07 #2
mattmao
121 100+
Thanks a lot. I've decided rewrite my code to fix this problem. The null terminator is really a big issue to think about...
Oct 18 '07 #3

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

Similar topics

9
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain...
2
by: daniel | last post by:
Hi everyone, I'm trying to get this program compiled under Solaris. I have actually no idea about programming, sorry to bother you. Unfortunately Solaris don't use the function strsep()...
4
by: collinm | last post by:
hi this is my code to analyse a file void analyzeFilename() { char string="B_L2_HLD_GRN_NOR_Run_Counter.txt"; char *tokenptr; char *seperators="_";
9
by: daniel | last post by:
Hi everyone, I'm trying to get this program compiled under Solaris. Unfortunately I have little experience with C. Solaris doesn't use the function strsep() anymore: char *strsep(char...
6
by: plmanikandan | last post by:
Hi, I need to split the value stored in a string and store them to another charrecter array.I am using strtok function.But i am getting invalid output when there is no value between delimiter ...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
16
by: Amit Gupta | last post by:
Hi - I get a seg-fault when I compile and run this simple program. (seg-fault in first call to strtok). Any clues? My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)" #include...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.