473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tokenizing a string using STRTOK() in g++

I'm trying to tokenize a C++ string with the following code:

#include <iostream>
#include <stdio>
#include <string>

using namespace std;
int main(int argc, char* argv[])
{
char *temp_string;
char *new_string;
char *delim = " ";

cout <<"Please enter word: ";
cin >> new_string;

temp_string = strtok(new_string, delim);
while (temp_string != NULL)
{
cout<<temp_string<<endl;
temp_string = strtok(NULL, delim);
}
}
Here's my output:
Please enter word: This is a test
This

I never get more than the first word out!!! I'm using "g++ file.cpp"
with no flags.

I'm certain the flags are somehow wrong - I can't seem to figure out
anything wrong with my code.

Please advise?!

Feb 1 '06 #1
11 3905
wreckingcru wrote:
I'm trying to tokenize a C++ string with the following code:

If you want you can check
http://www.boost.org/libs/tokenizer/tokenizer.htm . It has a
implementation with iterators.

-- Nitin Motgi

Feb 1 '06 #2
wreckingcru wrote:
I'm trying to tokenize a C++ string with the following code:

#include <iostream>
#include <stdio>
#include <string>

using namespace std;
int main(int argc, char* argv[])
{
char *temp_string;
char *new_string;
char *delim = " ";

cout <<"Please enter word: ";
cin >> new_string;


The call above only reads in in "This ". The new_string only contains
only "This" that's why you are seeing only that output. You can use
cin.getline(new_string, length);

-- Nitin Motgi

Feb 1 '06 #3
wreckingcru wrote:
I'm trying to tokenize a C++ string with the following code:

#include <iostream>
#include <stdio>
#include <string>

using namespace std;
int main(int argc, char* argv[])
{
char *temp_string;
char *new_string;
char *delim = " ";

cout <<"Please enter word: ";
at this point new_string points to garbage. The next statement will
invoke UB. cin >> new_string; Congratulations, you have invoked UB.

temp_string = strtok(new_string, delim);
while (temp_string != NULL)
{
cout<<temp_string<<endl;
temp_string = strtok(NULL, delim);
}
}

Try this instead

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::cout << "Please enter some text: ";

std::string the_line;
if (std::getline(std::cin, the_line))
{
std::istringstream is(the_line);
std::string temp_str;
while (is >> temp_str)
std::cout << temp_str << std::endl;
}
}
Feb 1 '06 #4

wreckingcru wrote:
I'm trying to tokenize a C++ string with the following code:

#include <iostream>
#include <stdio>
#include <string>

using namespace std;
int main(int argc, char* argv[])
{
char *temp_string;
char *new_string;
char *delim = " ";

cout <<"Please enter word: ";
cin >> new_string;

-----> 1)Don't try to read data into an uninitialized pointer.
-----> 2)cin stops reading data when it encounters a space.
-----> 3)try cout<<new_string;
This will output "This" and not "This is a test"

temp_string = strtok(new_string, delim);
while (temp_string != NULL)
{
cout<<temp_string<<endl;
temp_string = strtok(NULL, delim);
}
}
Here's my output:
Please enter word: This is a test
This

I never get more than the first word out!!! I'm using "g++ file.cpp"
with no flags.

I'm certain the flags are somehow wrong - I can't seem to figure out
anything wrong with my code.

Please advise?!


Feb 1 '06 #5
>char *temp_string;
char *new_string;


The above two variables need to be allocated memory dynamically using
new operator and also don't forget to free them at the end.

--Wg-

Feb 1 '06 #6
Ok thanks for the help! I realize that cin stops at the first space.

Anyway, I don't even want it to take input from user prompt - I was
just using that for testing purposes.

Here's the problem though - the input will be coming from a string
object ..and strtok() only accepts char* - I tried typecasting it
explicitly but it doesn't like that for sure.

What's the fix for tokenizing this string object?

Feb 1 '06 #7

wreckingcru wrote:
Ok thanks for the help! I realize that cin stops at the first space.

Anyway, I don't even want it to take input from user prompt - I was
just using that for testing purposes.

Here's the problem though - the input will be coming from a string
object ..and strtok() only accepts char* - I tried typecasting it
explicitly but it doesn't like that for sure.
Never "try" casting to make something work. Only ever cast when you
know precisely why the code doesn't compile without the cast, precisely
what the cast will do *and* precisely why it is OK to overrule the
compiler in that way in that situation.
What's the fix for tokenizing this string object?


red floyd has already posted a solution using stringstreams.

If you really want to use strtok, you will need to copy the data from
the string into a char array. Note that just passing the result of the
string member function c_str() to strtok is not sufficient because
strtok needs to be able to modify its argument.

Rather than going through this process with every string you need to
tokenise, write a function to do the work that takes a string and
returns whatever you want to return.

Gavin Deane

Feb 1 '06 #8
Gavin Deane wrote:

Rather than going through this process with every string you need to
tokenise, write a function to do the work that takes a string and
returns whatever you want to return.


Something like this:

#include <string>
#include <vector>
#include <sstring>

std::vector<std::string> tokenize(const std::string& the_str)
{
std::istringstream is(the_str);
std::vector<std::string> v;
std::string tmp;
while (is >> tmp)
v.push_back(tmp);
return v;
}

Feb 1 '06 #9

red floyd wrote:
Gavin Deane wrote:

Rather than going through this process with every string you need to
tokenise, write a function to do the work that takes a string and
returns whatever you want to return.

Something like this:


Something like that, yes ...
#include <string>
#include <vector>
#include <sstring>


.... except you meant <sstream> :-)

<snip>

Gavin Deane

Feb 1 '06 #10

wreckingcru wrote:
I'm trying to tokenize a C++ string with the following code:

#include <iostream>
#include <stdio>

[snip]

it should be
#include <cstdio>
or
#include <stdio.h>

HTH,

Marcelo Pinto

Feb 1 '06 #11
Gavin Deane wrote:
[redacted]
... except you meant <sstream> :-)


Yes, I did. That's what happens when you type on the fly :(
Feb 1 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
I'm writing a snippet of code intended to parse a grid that is represented by a long string. Each row of the grid is deliminated in the string with a '~' character. Inside each row substring,...
28
by: David Rubin | last post by:
I looked on google for an answer, but I didn't find anything short of using boost which sufficiently answers my question: what is a good way of doing string tokenization (note: I cannot use boost)....
6
by: Alex Vinokur | last post by:
Here is some program with using strtok() and std::string. Why does strtok() affect str2 in function func2()? ====== File foo.cpp : BEGIN ====== #include <cstring> #include <string> #include...
9
by: Java and Swing | last post by:
Say I have a string which contains numbers separated by a comma... such as "0,1,2,3,4,5"...I want to split the string at the commas and return an array containing, 0,1...5. Suggestions? I've...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
14
by: Erik | last post by:
Hi, i'm trying to do this : #include <stdlib.h> #include <stdio.h> #define FILE "/tmp/myfile" #define USERS_LIST "/tmp/userslist" int main() { //open file
29
by: Andrea | last post by:
I want to write a program that: char * strplit(char* str1, char *str2, char * stroriginal,int split_point) that take stroriginal and split in the split_point element of the string the string...
8
by: Acolyte | last post by:
Ok, the program I'm working on now involves taking an input string and tokenizing it, by seperating it by spaces. Here's what I've got: #include <stdio.h> #include <string.h> int main (void) {...
6
by: Studlyami | last post by:
Okay, i have developed a file parser in C++ that i am trying to being into a c# program which is proving to be a lot more difficult than i thought. first i scan a file (which i opened using a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.