Here is my code, just to test the strtok functionality: - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
struct sampleClass
-
{
-
int num;
-
char *text;
-
struct sampleClass *next;
-
};
-
-
typedef struct sampleClass node;
-
-
void fillinnode(node *target, int num, char **text);
-
void printoutnodes(node *startnode);
-
-
-
int main()
-
{
-
char text[30];
-
char *token1, *token2, *token3;
-
int i;
-
-
node *a=(node *) malloc(sizeof(node));
-
node *b=(node *) malloc(sizeof(node));
-
node *c=(node *) malloc(sizeof(node));
-
-
a->next = b;
-
b->next = c;
-
c->next = NULL;
-
-
for(i=0; i<30; i++)
-
{
-
if (i%10 == 0&&i!=0)
-
text[i]=':';
-
else text[i]='*';
-
}
-
-
printf("original string is");
-
for(i=0; i<30; i++)
-
printf("%c", text[i]);
-
printf("\n");
-
-
token1 = strtok(text, ":");
-
printf("token1=%s\n", token1);
-
-
token2 = strtok(NULL, ":");
-
printf("token2=%s\n", token2);
-
-
token3 = strtok(NULL, "");
-
printf("token3=%s\n", token3);
-
-
fillinnode(a, 1, &token1);
-
fillinnode(b, 2, &token2);
-
fillinnode(c, 3, &token3);
-
-
printoutnodes(a);
-
}
-
-
void fillinnode(node *target, int num, char **text)
-
{
-
target->num = num;
-
target->text = *text;
-
}
-
-
void printoutnodes(node *startnode)
-
{
-
node *temp = startnode;
-
while(temp!= NULL)
-
{
-
printf("%d", temp->num);
-
printf("%s", temp->text);
-
temp = temp->next;
-
}
-
}
It compiled well with gcc and runs. However, the last string token is not closed well, see the output: - charlie$ gcc why.c
-
charlie$ ./a.out
-
original string is**********:*********:*********
-
token1=**********
-
token2=*********
-
token3=*********
-
1**********2*********3*********��charlie$
-
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?
2 3417
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.
Thanks a lot. I've decided rewrite my code to fix this problem. The null terminator is really a big issue to think about...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
|
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);
|
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="_";
|
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);
|
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()
| |
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){
|
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()
|
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...
|
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"
|
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...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |