Strtok function....


 
Thread Tools Search this Thread
Operating Systems Linux Strtok function....
# 1  
Old 01-24-2008
Strtok function....

can any help me out y dis program is giving me a segmentation fault.....

#include<stdio.h>
#include<string.h>
int main()
{
char *str="Tanvir/home/root/hello";
const char *d ="/";
char *ret;


ret=strtok(str,d);
if(ret==NULL)
printf("NULL NULL");
else
printf("\n%c",ret);

return 0;
}
# 2  
Old 01-24-2008
Quote:
Originally Posted by Tanvirk
can any help me out y dis program is giving me a segmentation fault.....

#include<stdio.h>
#include<string.h>
int main()
{
char *str="Tanvir/home/root/hello";
const char *d ="/";
char *ret;


ret=strtok(str,d);
if(ret==NULL)
printf("NULL NULL");
else
printf("\n%c",ret);

return 0;
}
In this case str points to location that has the initialized value which could be either ready only or read writable.If it happens to be on read only then the strtok function may not be able to modify the string and so generates the SEGV

You could change that to,
Code:
char  str[]="Tanvir/home/root/hello";

Thanks
Nagarajan G
# 3  
Old 01-24-2008
Quote:
Originally Posted by ennstate
In this case str points to location that has the initialized value which could be either ready only or read writable.If it happens to be on read only then the strtok function may not be able to modify the string and so generates the SEGV

You could change that to,
Code:
char  str[]="Tanvir/home/root/hello";

Thanks
Nagarajan G
Dear Nagarajan,

Thanks for ur detailed explaination of strtok.
I fully agree with ur comment.Smilie

but however strtok should not be used in threads. can u explain me why?
they say in such case its safe to use strtok_r().

Regards
-Ashok Meti
# 4  
Old 01-24-2008
Thanks a lot Nagrajan and Ashok !!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Programming

strtok() gives segmentation fault!!

#include<iostream.h> #include<string> #include<stdio.h> int main() { char *cmd="delete backup backup-iso image a.iso b.iso c.iso d.iso"; char *tokenized_cmd,*sub_cmd; sub_cmd=strstr(cmd,"image"); tokenized_cmd=strtok(sub_cmd," "); ... (3 Replies)
Discussion started by: ashwini.engr07
3 Replies

4. Programming

strtok with while loops

Why is line (null) after the first while loop run? (keyword does jump to the next word.) #include <ftw.h> #include <stdio.h> #include <string.h> char filenames = ""; int list(const char *name, const struct stat *status, int type) { if( (type == FTW_F) && strstr(name, ".txt") &&... (3 Replies)
Discussion started by: cyler
3 Replies

5. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

6. Programming

How to use strtok twice in the same program?

string str1(" 1 2 3 4 512543 "); string str2; if(str2.empty()) str2=str1; cout << "str2:" <<str2 <<endl; p1=strtok((char *)str1.c_str()," "); while(p1) { ... (3 Replies)
Discussion started by: sathishkmrv
3 Replies

7. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

8. Shell Programming and Scripting

strtok equivalent in perl

Hi All, Is their any equivalent for strtok (in c) to use in perl script. Thanks in advance. JS (1 Reply)
Discussion started by: jisha
1 Replies

9. Programming

Regardign strtok() output directing to 2-D string array

Hi, I just wrote a program in C to split a comma seperated string in to group of strings using strtok() function. The code is: int main() { char *temp;//not used here but basically we extract one string after another using strtok() and assign to a string pointer defined like this. ... (3 Replies)
Discussion started by: SankarV
3 Replies

10. Programming

better way than strtok?

Hi all, Right now I'm using this but it seems to be a hack: if (prefix(arg, "mark=")) { for (markid = strtok(args,"="); markid; markid=strtok((char *)NULL, "=")) { basically the user passes "mark=ny" to the command. I want to be able to extract "ny" from that... (7 Replies)
Discussion started by: annie
7 Replies
Login or Register to Ask a Question