Click to See Complete Forum and Search --> : Anyone know how to use strtok for C?
digitalzero
03-29-2002, 02:12 AM
I've been trying to get strtok to work and I always get the Segmentation Fault error. Anyone able to help?
This is what i've written:
#include <stdio.h>
#include <string.h>
main()
{
char *line;
char *token;
char *delim;
delim = " "; /* a space */
line = "hello this is a test";
token = strtok(line,delim);
printf("%s\n",token);
}
The program compiles fine. But when I run it, it says SEGMENTATION FAULT.
What's going on?
Skroob
03-29-2002, 02:20 AM
You need to malloc space for the line, delim, token.
tecknophreak
03-29-2002, 03:28 AM
Originally posted by digitalzero:
<STRONG>
#include <stdio.h>
#include <string.h>
main()
{
char *line;
char *token;
char *delim;
delim = " "; /* a space */
line = "hello this is a test";
token = strtok(line,delim);
printf("%s\n",token);
}
The program compiles fine. But when I run it, it says SEGMENTATION FAULT.
What's going on?</STRONG>
a char *line="hello this is a test";
should do the trick for you.
or...use string(#include <string> )
digitalzero
03-29-2002, 07:31 AM
the char *line = "blah blah blah";
doesnt work.
I didnt know u could declare and define a variable in one expression....
:(
marvin
03-29-2002, 07:33 AM
The "Hello this is a test" is a constant string. The strtok function will try to modify the constant string which leads to a segfault.
Use
char line[] = "Hello this is a test";
instead. This will create a non-constant string.
digitalzero
03-29-2002, 07:40 AM
Still doenst work...
[ 29 March 2002: Message edited by: digitalzero ]
marvin
03-29-2002, 09:42 AM
That is strange :confused: It compiles and runs for me.
#include <stdio.h>
#include <string.h>
int main()
{
char line[] = "hello this is a test";
char *token;
const char *delim = " "; /* a space */
token = strtok(line, delim);
if (token == NULL)
return 1;
do
{
printf("%s\n",token);
token = strtok(NULL, delim);
} while (token != NULL);
return 0;
}
bwkaz
03-29-2002, 09:44 AM
Originally posted by marvin:
<STRONG>Use
char line[] = "Hello this is a test";
instead. This will create a non-constant string.</STRONG>
No, that will create a constant string, but then make line be a constant pointer to it.
What you need is something like this:
char temp[] = "Hello this is a test";
char *line = (char *)malloc(strlen(temp) * sizeof(char));
strncpy(line, temp, strlen(temp));
/* Now try strtok with line */
free(line);
This way, line isn't const and the string it points to isn't const either. You make a copy of the const string and put it in line.
marvin
03-29-2002, 10:43 AM
No,
char line[] = "Some string"
is not a string constant. line is a pointer to an array of 12 char
It is basically the same as
char line[12] = "Some string"
It is legal to modify any element of the char array.
bwkaz
03-29-2002, 06:42 PM
...
:o
Yeah, you're right. You can modify its characters. But according to the strtok manpage, it won't work on constant strings (which might not even apply here... but I'm not sure), so you may want to try the strcpy() version anyway, just to make sure that's not it.
Also, what compiler are you using (gcc --version to find out)? If it's RedHat 7's, there are bugs in it.
digitalzero
03-29-2002, 07:28 PM
thanks all.
Marvin's bit of code worked out!!!
digitalzero
03-30-2002, 08:25 PM
When I set the deliminator to be a space... that is " ". For some reason it doesnt tokenize appropriately. For example:
"Hello World"
it would only take the first token. The second token could not be read. This is my code:
char currentLine[100];
delim = " ";
scanf("%s",¤tLine);
currentToken = strtok(currentLine,delim);
do
{
printf("%s ", currentToken);
} while((currentToken = strtok(NULL,delim)) != NULL);
putchar('\n');
Please help again.
marvin
03-30-2002, 10:36 PM
scanf("%s", currentLine); only reads the first word, it stops when it finds the white space. Use something like
fgets(currentLine, 100, stdin);
instead. This will also have the benefit that it doesn't read more characters than can fit in your buffer.
be careful with strtok... I can't remember why right now; I just remember that you have to be careful... maybe I'll remember why if I think about it some more...
Stuka
04-01-2002, 07:20 PM
kmj-
The only reason I can think of is because strtok() uses static variables - inherently thread-unsafe (or is that unthread-safe?).
Originally posted by Stuka:
<STRONG>kmj-
The only reason I can think of is because strtok() uses static variables - inherently thread-unsafe (or is that unthread-safe?).</STRONG>
Yeah; that was it. It's not threadsafe.
bwkaz
04-01-2002, 09:52 PM
Not to mention (from the strtok manpage):
BUGS
Never use these functions. If you do, note that:
These functions modify their first argument.
The identity of the delimiting character is lost.
These functions cannot be used on constant strings.
Then it also says they're not thread-safe, but that if you use strtok_r, that fixes that. The difference is that strtok_r gets passed the address of a string to use a temporary space, read its man page for details.
digitalzero
04-03-2002, 07:14 AM
Thanks for the reply all! Yah strtok does the first token. I learned this the hard way...