473,796 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with the last string token divided by strtok.

121 New Member
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 3417
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 New Member
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
18432
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 word char *mystring "Jane and Peter and Tom and Cindy" char *delim = " and "; char *token; token = strtok(mystring, delim);
2
6489
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() anymore: char *strsep(char **stringp, const char *delim);
4
3544
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
4009
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 **stringp, const char *delim);
6
1097
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 my code #include<stdio.h> #include<string.h> #include<stdlib.h> void main()
4
2736
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 somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
16
2044
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 <string.h> int main()
9
2519
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 *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
11
904
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", "passwort" from "users" order by "users"
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.