473,386 Members | 1,795 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,386 software developers and data experts.

Using strtok, need to insert null into vector

I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Thanks

Jul 23 '05 #1
5 3845
kr*******@yahoo.com wrote:
I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Thanks


Yeah, don't use strtok. :( The definition of strtok says that multiple
delimiters will be rolled into one. You might get your test into a
std::string, and use find_first_of()
Jul 23 '05 #2
krini_...@yahoo.com wrote:
I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?


First, you need to read the FAQ, paying special attention to section
5.8. Then, feel free to post again and we'll be glad to help.

Kristo

Jul 23 '05 #3


kr*******@yahoo.com wrote:
I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Here is one I've worked up for this sort of task. If you need
strtok()'s ability to have multiple delimiters, then you'll have to
work it out yourself.

#include <vector>
#include <string>
#include <sstream>

using namespace std;

void Explode(const string &inString, vector<string> &outVector, char
separator)
{
stringstream inStream(inString);
string tempString;

while (getline(inStream, tempString, separator))
outVector.push_back(tempString);
}

int main()
{
string s = "one,two,three,,five";
vector<string> vec;

Explode (s, vec, ',');
for (int i = 0; i < vec.size(); i++)
cout << i << ": " << vec[i] << endl;

return 0;
}
Result:

0: one
1: two
2: three
3:
4: five


Brian

Jul 23 '05 #4
kr*******@yahoo.com wrote:
I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Thanks


Look at
http://groups-beta.google.com/*group...a5c63*35?hl=en
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #5
Thank you for the help. It works. I appreciate it!

Default User wrote:
kr*******@yahoo.com wrote:
I have a string that is delimited by commas. I'm using strtok and
putting the values in a vector. In some cases, I may have 2 commas side
by side and therefore need it to insert a null value. Right now, when
it encounters this it puts the next value in the vector and throws
everything else off. Does anyone know how I can insert a null value
when tokenizing a string?

Here is one I've worked up for this sort of task. If you need
strtok()'s ability to have multiple delimiters, then you'll have to
work it out yourself.

#include <vector>
#include <string>
#include <sstream>

using namespace std;

void Explode(const string &inString, vector<string> &outVector, char
separator)
{
stringstream inStream(inString);
string tempString;

while (getline(inStream, tempString, separator))
outVector.push_back(tempString);
}

int main()
{
string s = "one,two,three,,five";
vector<string> vec;

Explode (s, vec, ',');
for (int i = 0; i < vec.size(); i++)
cout << i << ": " << vec[i] << endl;

return 0;
}
Result:

0: one
1: two
2: three
3:
4: five


Brian


Jul 23 '05 #6

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

Similar topics

6
by: gyan | last post by:
Hi How strtok track through string? char *strtok(char *s1, const char *s2); As i know The first call (with pointer s1 specified) returns a pointer to the first character of the first...
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
17
by: bofh1234 | last post by:
I need to delimit a string. The delimiters are a semicolon and comma. When I run the program I get a segmentation fault on the first strtok. I followed the examples of others and from my old C...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
5
by: Kelly B | last post by:
I need a function which returns me a "word" from a given string and then sets the pointer to the next one which is then retrieved during further calls to the function. I think strtok( ) is the...
4
by: ohaqqi | last post by:
Hi everybody. I haven't programmed anything in about 8 years, I've read up a little bit on C and need to write a shell in C. I want to use strtok() to take an input from a user and parse it into the...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
10
by: sagitalk | last post by:
Here is my code: #include <iostream> #include <vector> #include <fstream> #include <stdio.h> #include <string> #include <cstring> using namespace std;
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
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,...

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.