Sponsored Content
Top Forums Programming How to use strtok twice in the same program? Post 302243187 by sathishkmrv on Saturday 4th of October 2008 03:02:45 AM
Old 10-04-2008
How to use strtok twice in the same program?

Code:
       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)
                {
                        v1.push_back(atoi(p1));
                cout << "val of p1 " << p1 << endl;
                 p1=strtok(NULL," ");
                }
                cout << "size of v1 " << v1.size() <<endl;
                p2=strtok((char *)str2.c_str()," ");
                cout << "str2:" <<str2 <<endl;
                while(p2)
                {
                        v2.push_back(atoi(p2));
                        cout << "val of p2 " << p2 <<endl;
                        p2=strtok(NULL," ");
                }

                cout << "size of v2" << v2.size() <<endl;

I get the following o/p.

Code:
str2: 1 2 3 4 512543 
val of p1 1
val of p1 2
val of p1 3
val of p1 4
val of p1 512543
size of v1 5
str2: 1234512543
val of p2 1
after tok p2  
size of v21

I want str2 also to be tokenized. But strtok after reaching NULL once it returns only NULL pointer everytime when it is accessed using NULL.

How can I tokenize str2 now?

Thanks
 

10 More Discussions You Might Find Interesting

1. 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

2. Linux

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 ... (3 Replies)
Discussion started by: Tanvirk
3 Replies

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

6. 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

7. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

8. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

9. 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

10. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies
PROP_STRING(3)						   BSD Library Functions Manual 					    PROP_STRING(3)

NAME
prop_string, prop_string_create, prop_string_create_cstring, prop_string_create_cstring_nocopy, prop_string_copy, prop_string_copy_mutable, prop_string_size, prop_string_mutable, prop_string_cstring, prop_string_cstring_nocopy, prop_string_append, prop_string_append_cstring, prop_string_equals, prop_string_equals_cstring -- string value property object LIBRARY
Property Container Object Library (libprop, -lprop) SYNOPSIS
#include <prop/proplib.h> prop_string_t prop_string_create(void); prop_string_t prop_string_create_cstring(const char *cstring); prop_string_t prop_string_create_cstring_nocopy(const char *cstring); prop_string_t prop_string_copy(prop_string_t string); prop_string_t prop_string_copy_mutable(prop_string_t string); size_t prop_string_size(prop_string_t string); bool prop_string_mutable(prop_string_t string); char * prop_string_cstring(prop_string_t string); const char * prop_string_cstring_nocopy(prop_string_t string); bool prop_string_append(prop_string_t str1, prop_string_t str2); bool prop_string_append_cstring(prop_string_t string, const char *cstring); bool prop_string_equals(prop_string_t str1, prop_string_t str2); bool prop_string_equals_cstring(prop_string_t string, const char *cstring); DESCRIPTION
The prop_string family of functions operate on a string value property object type. prop_string_create(void) Create an empty mutable string. Returns NULL on failure. prop_string_create_cstring(const char *cstring) Create a mutable string that contains a copy of cstring. Returns NULL on failure. prop_string_create_cstring_nocopy(const char *cstring) Create an immutable string that contains a reference to cstring. Returns NULL on failure. prop_string_copy(prop_string_t string) Copy a string. If the string being copied is an immutable external C string reference, then the copy is also immutable and references the same external C string. Returns NULL on failure. prop_string_copy_mutable(prop_string_t string) Copy a string, always creating a mutable copy. Returns NULL on failure. prop_string_size(prop_string_t string) Returns the size of the string, not including the terminating NUL. If the supplied object isn't a string, zero is returned. prop_string_mutable(prop_string_t string) Returns true if the string is mutable. If the supplied object isn't a string, false is returned. prop_string_cstring(prop_string_t string) Returns a copy of the string's contents as a C string. The caller is responsible for freeing the returned buffer. In user space, the buffer is allocated using malloc(3). In the kernel, the buffer is allocated using malloc(9) using the malloc type M_TEMP. Returns NULL on failure. prop_string_cstring_nocopy(prop_string_t string) Returns an immutable reference to the contents of the string as a C string. If the supplied object isn't a string, NULL is returned. prop_string_append(prop_string_t str1, prop_string_t str2) Append the contents of str2 to str1, which must be mutable. Returns true upon success and false otherwise. prop_string_append_cstring(prop_string_t string, const char *cstring) Append the C string cstring to string, which must be mutable. Returns true upon success and false otherwise. prop_string_equals(prop_string_t str1, prop_string_t str2) Returns true if the two string objects are equivalent. prop_string_equals_cstring(prop_string_t string, const char *cstring) Returns true if the string's value is equivalent to cstring. SEE ALSO
prop_array(3), prop_bool(3), prop_data(3), prop_dictionary(3), prop_number(3), prop_object(3), proplib(3) HISTORY
The proplib property container object library first appeared in NetBSD 4.0. BSD
January 21, 2008 BSD
All times are GMT -4. The time now is 08:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy