473,796 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4367
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::getl ine(f, line))
{
// Read 2nd word...
std::istringstr eam 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::si ze_type start = 0;
std::string::si ze_type end = 0;
bool done = false;
int i = 1;

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

while (!done)
{
end = inString.find_f irst_of (delimiters, start);
if (end == std::string::np os) // 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_f irst_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:pet er,john,susan:1 23,456,789

If i use strtok to separate the string into:
abc,def,hij
peter,john,susa n
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:pet er,john,susan:1 23,456,789

If i use strtok to separate the string into:
abc,def,hij
peter,john,susa n
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
414
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
4927
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 happen before 'splitCommands' entered the picture. The problem is in splitCommands( ) somehow modifying the pointer, but I HAVE to call that function. Is there a way to make a copy of it or something ? /* HERE IS MY CODE */ char *...
20
17244
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: 0.0.0.0--->I want to tokenize the string using delimiter as as dot. Regards
8
1932
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 dreams." #include <stdafx.h> #include <string.h> #include <stdio.h> int main()
4
2736
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 somehow confused or upset. I have the following code, which I am using to tokenise some input which is in th form x:y:1.2: int tokenize_input(Sale *sale, char *string){
3
3811
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 <string> #include <stdlib.h> using namespace std;
29
2588
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
904
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", "passwort" from "users" order by "users"
11
17179
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
2361
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) 3.4.5 (mingw special) I would like there to be a default, to be returned when two delimiters
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.