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

strsep() function problem, please help

Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
..
..
..

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan

Nov 14 '05 #1
9 3991
Try this code.

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
strtok(w, "\t");
if ((p = strtok(NULL, " \t")) == NULL)
continue;

<da****@best.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.
.

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan

Nov 14 '05 #2
Of course, strtok(w, "\t"); should be replaced with strtok(w, " \t"); in the
3rd line of my code.

"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Try this code.

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
strtok(w, "\t");
if ((p = strtok(NULL, " \t")) == NULL)
continue;

<da****@best.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.
.

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan


Nov 14 '05 #3
"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Of course, strtok(w, "\t"); should be replaced with strtok(w, " \t"); in
the 3rd line of my code.
Of course, your solution isn't really a solution regardless of the 3rd line!
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.


while(fgets(buf, sizeof buf, desc)) {
if((w = strcspn(buf, " \t")) == NULL)
continue;
*w++ = 0;
p = buf;
...
}

Untested, but it should work.
Regards,
Mark

"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Try this code.

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
strtok(w, "\t");
if ((p = strtok(NULL, " \t")) == NULL)
continue;

<da****@best.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.
.

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan



Nov 14 '05 #4

"Mark" <so***@localbar.com> wrote in message
news:Bp****************@newshog.newsread.com...
"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Of course, strtok(w, "\t"); should be replaced with strtok(w, " \t"); in
the 3rd line of my code.
Of course, your solution isn't really a solution regardless of the 3rd
line!

And neither is mine!
should be: strpbrk() not strcspn() !
*oops* maybe I should have tested it first!

while(fgets(buf, sizeof buf, desc)) {
if((w = strpbrk(buf, " \t")) == NULL)
continue;
*w++ = 0;
p = buf;
...
}

Still untested ;) But much better...
Mark
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.


while(fgets(buf, sizeof buf, desc)) {
if((w = strcspn(buf, " \t")) == NULL)
continue;
*w++ = 0;
p = buf;
...
}

Untested, but it should work.
Regards,
Mark

"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Try this code.

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
strtok(w, "\t");
if ((p = strtok(NULL, " \t")) == NULL)
continue;

<da****@best.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.
.

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan



Nov 14 '05 #5
On Tue, 14 Jun 2005 08:35:49 -0700, daniel wrote:
Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:


strsep() isn't a standard C library function but common implementations
work differently to strtok(), which is the reason for it in the first
place. strtok() treats a sequence of delimeter characters as a single
separator field e.g. using \t delimiters field1\t\tfield2 is 2 fields.
strsep() would produce 3 i.e. with a zero length middle field. If you need
this functionality then you can't use strtok().

A simple solution would be to grab source code for strsep() from the Web,
it is a small function and a Google search of strsep source gives lots
of matches.

Lawrence
Nov 14 '05 #6
da****@best.de wrote:

I'm trying to get this program compiled under Solaris.
Unfortunately I have little experience with C.

Solaris doesn't use the function strsep() anymore:

char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to
simply substitute strsep() with strtok() which didn't work. The
code which I need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.

Any help would be greatly appreciated since google and everyone
I know couldn't help.


It seems rather useless, since very few lines are going to use both
the space and a following tab as a token separator, which is what
you want according to your description of strsep. If that is the
logic you want look into strstr(), else look into strpbrk(), both
of which are standard functions.

I suspect what you really want is to find an occurrence of one or
more of those characters, after skipping leading occasions, and
that you don't want to use strtok because of it's re-entrancy
problems. You might want to simplify by making the delimiters any
whitespace, which will include \n and \f. So I would write a
routine to do this something like (untested):

/* This has the nuisance of strtok, it alters str content */
/* so it can't parse string constants, for example. However */
/* it doesn't have the reentrancy and threading problems */
char *sepstr(char* *str)
{
char *s = *str; /* scan off leading delims */
char *t; /* scan to next token */

while (isspace((unsigned char)*s)) s++;
if (!*s) return NULL;
t = s;
while (t && !isspace(unsigned char)*t) t++;
if (t) *t++ = '\0';
*str = t;
return s;
} /* untested */

I am not sure but that isspace is a C99 function; if so you may
have to build it out of the other is functions or first principles.

This may all be foolish because sscanf may also do the job for
you. However I rarely use the scanf family - it's too complicated
for me. Dan Pop would choose otherwise.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #7
I verified that my solution worked well, at least as I intended to. :-)

"Mark" <so***@localbar.com> wrote in message
news:Bp****************@newshog.newsread.com...
"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Of course, strtok(w, "\t"); should be replaced with strtok(w, " \t"); in
the 3rd line of my code.


Of course, your solution isn't really a solution regardless of the 3rd
line!
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.


while(fgets(buf, sizeof buf, desc)) {
if((w = strcspn(buf, " \t")) == NULL)
continue;
*w++ = 0;
p = buf;
...
}

Untested, but it should work.
Regards,
Mark

"thilbong" <ty****@vt.edu> wrote in message
news:d8**********@solaris.cc.vt.edu...
Try this code.

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
strtok(w, "\t");
if ((p = strtok(NULL, " \t")) == NULL)
continue;

<da****@best.de> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi everyone,

I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.

Solaris doesn't use the function strsep() anymore:
char *strsep(char **stringp, const char *delim);

DESCRIPTION
The strsep() function returns the next token from the
string stringp which is delimited by delim. The token is
terminated with a `\0' character and stringp is updated to
point past the token.

RETURN VALUE
The strsep() function returns a pointer to the token, or
NULL if delim is not found in stringp.

I need to use a function that Solaris knows about. Tried to simply
substitute strsep() with strtok() which didn't work. The code which I
need to port is:

while (fgets(buf, sizeof(buf), desc) != NULL) {
w = buf;
if ((p = strsep(&w, " \t")) == NULL)
continue;
.
.
.

Any help would be greatly appreciated since google and everyone I know
couldn't help.

-Dan



Nov 14 '05 #8
Thanky you very much for helping me out here, I was almost despairing.

So thanks for all the replies, the solution worked out very well!

Bye

-Dan

Nov 14 '05 #9
da****@best.de wrote:

Thanky you very much for helping me out here, I was almost despairing.

So thanks for all the replies, the solution worked out very well!

Bye


Too late, but here's what I got anyway:

#include <string.h>

char *str_sep(char **s1, const char *s2)
{
char *const p1 = *s1;

if (p1 != NULL) {
*s1 = strpbrk(p1, s2);
if (*s1 != NULL) {
*(*s1)++ = '\0';
}
}
return p1;
}

--
pete
Nov 14 '05 #10

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

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
2
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
2
by: daniel | last post by:
Hi everyone, I'm trying to get this program compiled under Solaris. I have actually no idea about programming, sorry to bother you. Unfortunately Solaris don't use the function strsep()...
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
5
by: mk | last post by:
Greetings all - im new to this newsgroup so pls dont flame me :p I need some help! Please view the html below in a browser. Or goto this url -http://firebrain.co.uk/java-problem.htm (Assuming...
3
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
9
by: shror | last post by:
hi every body, i have a problem which is when i was checking my mail() form it work fine but the problem is that the form configuration is not set correctly, in details: the from var is not...
8
by: cpptutor2000 | last post by:
Could some C guru please help me? I have a function that takes as an argument a pointer to an array of unsigned chars (basically a hex representation of a dotted decimal IP address). When I print...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
0
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.