- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- strtok question again
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:00 AM
04-29-2005 07:00 AM
strtok question again
I am trying to parse an ASCII file using strtok. The trouble is some string that I am parsing contain one space and I am trying to get them all at once:
From the string:
Hello how are you
I want the following:
Hello
how
are you
In 3 different strings........
So far I am using the following code:
char* pValue;
pValue = strtok (pText, " \t");
i = 0;
do
{
pValue = strtok (NULL, " \t");
i++;
} while( (pValue != NULL) && (i < Column) );
what I get is:
Hello
how
are
you.....
In fact, I would like strtok to differ from one space to two spaces....
getting only 3 strings not 4:
For the string:
"1234 1235 152153 1 111"
I would like to get only 4 strings:
"1234"
"1235"
"152153"
"1 111"
and not
"1"
- Tags:
- strtok
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:14 AM
04-29-2005 07:14 AM
Re: strtok question again
Did that help?
Looks like you need to modify the loop to the
following:
do
{
if (i < Column-1)
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, NULL);
i++;
} while( (pValue != NULL) && (i < Column) );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:19 AM
04-29-2005 07:19 AM
Re: strtok question again
The issue is that the single space could be anytim in the string, not only at the end.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:24 AM
04-29-2005 07:24 AM
Re: strtok question again
// scan to correct column
char* pValue;
pValue = strtok (pText, " \t");
i = 0;
do
{
pValue = strtok (NULL, NULL);
i++;
} while( (pValue != NULL) && (i < Column) );
This doesn't seem to work...any other idea?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:30 AM
04-29-2005 07:30 AM
Re: strtok question again
code as I suggested.
What is the value of Column?
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:35 AM
04-29-2005 07:35 AM
Re: strtok question again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:47 AM
04-29-2005 07:47 AM
Re: strtok question again
NULL terminated?
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:53 AM
04-29-2005 07:53 AM
Re: strtok question again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:54 AM
04-29-2005 07:54 AM
Re: strtok question again
- Biswajit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 07:59 AM
04-29-2005 07:59 AM
Re: strtok question again
tx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2005 08:08 AM
04-29-2005 08:08 AM
Re: strtok question again
#include
#include
int main ()
{
int i, Column;
char pText[512];
char *pValue;
/* Your first string */
i = 0;
strcpy (pText, "Hello how are you");
printf ("----> Your first string = \"%s\"\n", pText);
Column = 2; /* Set Column to # of words in string minus 2 */
pValue = strtok (pText, " \t");
printf ("pValue=\"%s\"\n", pValue);
do {
if (i < Column - 1 )
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, "\0");
i++;
printf ("pValue=\"%s\"\n", pValue);
} while( (pValue != NULL) && (i < Column) );
/* Your second string */
i = 0;
strcpy (pText, "1234 1235 152153 1 1111");
printf ("----> Your Second string = \"%s\"\n", pText);
Column = 3; /* Set Column to # of words in string minus 2 */
pValue = strtok (pText, " \t");
printf ("pValue=\"%s\"\n", pValue);
do {
if (i < Column - 1)
pValue = strtok (NULL, " \t");
else
pValue = strtok (NULL, "\0");
printf ("pValue=\"%s\"\n", pValue);
i++;
} while( (pValue != NULL) && (i < Column) );
return 0;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2005 03:20 AM
05-02-2005 03:20 AM
Re: strtok question again
Here is a screen capture of your code working....(see attached)
I want the strtok to differ when there is one space and 2 spaces, when there are 2 space, it is a different word, when it is 1 space it belongs to the same name...
"Hello how are you? good me too."
This will become:
Hello
how
are you?
good me
too.
As you can see, when there was more than one space, it considered it as 2 different string but when there was only one space the it put it together.
Thanks for your help already, I liked it the way you did the first program, looking for the end of string "\0".
rgds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2005 05:48 AM
05-02-2005 05:48 AM
Re: strtok question again
> I want the strtok to differ when there is one
> space and 2 spaces, when there are 2 space, it is
> a different word, when it is 1 space it belongs to
> the same name...
To do this, I would probably not use strtok. I would
use strchr() to search for single space char in a for
loop and when found, I would print and decide if I
need to print a space or a newline by looking at the
next char. Only pass the unprocessed part of the
input string as the first parameter to strchr().
I'm not loged into a HP-UX machine rightnow, so
can't give you an example code; but it should be
quite simple.
- Biswajit