473,324 Members | 2,567 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,324 software developers and data experts.

strtok ? ?

hi all, the strtok() cannot phrase the token within another token, am i
correct?

For example, i want to get the second word of every row of a file, how
to use strok to complete this?
thanks
from Peter (cm****@hotmail.com)

Feb 23 '06 #1
7 4343
Hi Peter,
For example, i want to get the second word of every row of a file, how
to use strok to complete this?


You might also consider a stream-based solution:

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

int main()
{
std::ifstream f("ReadMe.txt");

// Read all lines from test.txt...
std::string line;
while(std::getline(f, line))
{
// Read 2nd word...
std::istringstream tmp(line);
std::string first, second;
tmp >> first >> second;

std::cout << second << "\n";
}
}

Best regards,
Tilman
Feb 23 '06 #2
Peter wrote:
hi all, the strtok() cannot phrase the token within another token, am i
correct?
I presume you mean "parse."

If by "another token" you mean the delimeter, then yes, you need the
string to be parsed, and the delimeter to parse it.
For example, i want to get the second word of every row of a file, how
to use strok to complete this?


You decide what delimeter to use to divide the string into words.
Presumably a space. Then you read the file line by line, and
feed each line to strtok, with the space as delimeter, and call
it appropriately to get the second item. With appropriate code
around the call to take care of such things as there not being
two words on a line. You also decide what to do about such
weird cases as "my car,house,boat" etc., where there is some
strange thing with missing space. And so on.

Also, strtok has one really good use. It teaches newbies what
not to do, by biting them in the ass. Get hold of a good description
of strtok and read up on such things as: Buffer over runs as a
substantial security risk. Static buffers shared between different
calls to strtok as a data corruption risk. And so on.

When all of that gets on your nerves adequately, you think about
a stream based solution.
Socks

Feb 23 '06 #3
Peter wrote:
hi all, the strtok() cannot phrase the token within another token, am
i correct?

For example, i want to get the second word of every row of a file, how
to use strok to complete this?


Automatically? No. It's a fairly easy algorithm to write.

Generally, strtok() is not recommended. It works only on C-style
strings. It modifies the string. It can't be used with constant strings
or string literals. It has concurrency and reentrancey problems.

If all you need to do is get the second word of string, using
std::string makes a lot more sense.

Here's a little something I ginned up. Tested for functionality. May
have problems, review carefully before use.

#pragma warning( disable : 4786)

#include <vector>
#include <string>
// retrieves the nth substring, returns:
// 0 on success,
// -1 for invalid number
// 1 for insufficient number of substrings

int GetNthString (const std::string &inString,
const std::string delimiters,
std::string &outString,
int n)

{
int retVal = 0;
std::string tmpString;
std::string::size_type start = 0;
std::string::size_type end = 0;
bool done = false;
int i = 1;

if (n <= 0)
{
return -1;
}

while (!done)
{
end = inString.find_first_of (delimiters, start);
if (end == std::string::npos) // end of string
{
done = true;
}

if (i == n)
{
outString = inString.substr (start, end-start);
done = true;
}

else
{
if (done)
{
retVal = 1;
}
else
{
start = inString.find_first_not_of (delimiters, end);
i++;
}
}
}

return retVal;
}
#include <iostream>
using namespace std;

int main()
{
string s1 = "one and two and three";
string s2;
int res;

res = GetNthString (s1, " \t", s2, 2);

if (res == 0)
{
cout << s2 << endl;
}

else
{
cout << "Error: " << res << endl;
}

return 0;
}

Feb 23 '06 #4
Default User wrote:

Here's a little something I ginned up. Tested for functionality. May
have problems, review carefully before use.

#pragma warning( disable : 4786)
Urg. Didn't mean to paste that in. Can you tell I use VC++ 6? Sorry gnu
people.
#include <vector>


This wasn't needed, left over from the program I savaged to make the
new one.

Brian
Feb 23 '06 #5
Sorry, forgive my bad english.
What i mean is,if i want to get the second word of every delimiter ":",
i cannot use strtok, right?

For example, here is my testing char *:
abc,def,hij:peter,john,susan:123,456,789

If i use strtok to separate the string into:
abc,def,hij
peter,john,susan
123,456,789
Then i cannot use strtok to parse again on each substring.

And i want to get every second word of every ":", how to? suppose the
answer is def, john and 456.
In my os library, i only have strtok, and i found strtok is not working
in this case.
thanks
from Peter (cm****@hotmail.com)

Feb 24 '06 #6
Thank you all guys.
I really need a string class in my os.
thanks again

Feb 24 '06 #7
Peter wrote:
Sorry, forgive my bad english.
What i mean is,if i want to get the second word of every delimiter
":", i cannot use strtok, right?

For example, here is my testing char *:
abc,def,hij:peter,john,susan:123,456,789

If i use strtok to separate the string into:
abc,def,hij
peter,john,susan
123,456,789
Then i cannot use strtok to parse again on each substring.
Why not? Just start a new strtok sesssion (so to speak) with the
pointer to the substring you want to use and the new delimiter.
And i want to get every second word of every ":", how to? suppose the
answer is def, john and 456.
In my os library, i only have strtok, and i found strtok is not
working in this case.


It's not normally the "os libary" that is of interest, it's the
standard library of the implementation. An OS library may be used by
the implementation, of course.

What compiler are you using? std::string was in the 1998 C++ standard,
and most compilers of recent vintage provide it.

You will be better off if you use it with something like the code I and
another person posted yesterday, rather than using the rather tricky
strtok().

Brian
Feb 24 '06 #8

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

Similar topics

2
by: Ram Laxman | last post by:
Hi all, I have written the following code: /* strtok example */ #include <stdio.h> #include <string.h> static const char * const resultFileName = "param.txt";
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
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:...
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...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.