problem parsing strings

U

Ulrich Vollenbruch

Hi all!
since I'am used to work with matlab for a long time and now have to work
with c/c++, I have again some problems with the usage of strings, pointers
and arrays. So please excuse my basic question:

I want to parse a string like "3.12" to get two integers 3 and 12. I wanted
to use the function STRTOK()
I wrote a main- and a subfunction like:

main() {
int wl=0, il=0;
char *StrIn;

StrIn = "3.12";
printf("%s \n",StrIn);
str2prec(StrIn, wl, il);
}

/******************************************/
void str2prec(char *str_p, int wl, int iwl){
char str_test1[] ="3.12"; //just for testing
char *str_test2 ="3.12"; //just for testing
char *str_split;

printf ("Splitting string \"%s\" in tokens:\n",str_p);
str_split = strtok (str_p,".");
while (pch != NULL)
{
printf ("%s\n",str_split);
str_split = strtok (NULL, " ,.");
}
}

When I use str_p or str_test2 together with STRTOK() this will cause a
memory-error. With str_test1 it works?! Can someone tell why and how I can
manage this problem.
Anyway I would also be happy to hear of some other solutions for string
parsing...

Thanx for any help!
uli
 
N

nrk

Ulrich said:
Hi all!
since I'am used to work with matlab for a long time and now have to work
with c/c++, I have again some problems with the usage of strings, pointers
and arrays. So please excuse my basic question:

I want to parse a string like "3.12" to get two integers 3 and 12. I
wanted to use the function STRTOK()

You are probably better off using strtol or strtoul here instead of strtok
and then trying to do the parsing yourself. For instance:

long l1, l2;
char *endptr;
char *s = "3.12";

l1 = strtol(s, &endptr, 10);
if ( *endptr ) ++endptr;
l2 = strtol(endptr, &endptr, 10);
/* now l1 and l2 will have 3 & 12 resp. */

Read your compiler documentation or man pages for more info. on
strtol/strtoul.
I wrote a main- and a subfunction like:

main() {
int wl=0, il=0;
char *StrIn;

StrIn = "3.12";
printf("%s \n",StrIn);
str2prec(StrIn, wl, il);
}

/******************************************/
void str2prec(char *str_p, int wl, int iwl){
char str_test1[] ="3.12"; //just for testing
char *str_test2 ="3.12"; //just for testing
char *str_split;

printf ("Splitting string \"%s\" in tokens:\n",str_p);
str_split = strtok (str_p,".");
while (pch != NULL)
{
printf ("%s\n",str_split);
str_split = strtok (NULL, " ,.");
}
}

When I use str_p or str_test2 together with STRTOK() this will cause a
memory-error. With str_test1 it works?! Can someone tell why and how I
can manage this problem.

The problem is that strtok expects a modifiable string when it is first
called, and both str_p and str_test2 happen to be pointers to *constant*
strings. This means you're not allowed to modify them, and therefore
cannot pass them to strtok. However, str_test1 happens to be an array of
characters that can be modified. So, when you call strtok with str_test1
everything works. The solution is to either:
a) Make a copy of constant strings into a local array (possibly dynamically
allocated) and pass the copy to strtok instead.
b) Avoid strtok and use strtol/strtoul instead. Since neither of them
modify the source string, they are safe to use with constant strings.

-nrk.
 
U

Uli \(uvb\)

Dear j & nrk,
thank you for your prompt feedback! It was the perfect answer to get me out
a fix! And now I could found it also in my Stroustrup. Its really hard to
get in c after that long time *g* _uli

--
***
For reply remove NOSPAM_ from email-adress
***

nrk said:
Ulrich said:
Hi all!
since I'am used to work with matlab for a long time and now have to work
with c/c++, I have again some problems with the usage of strings, pointers
and arrays. So please excuse my basic question:

I want to parse a string like "3.12" to get two integers 3 and 12. I
wanted to use the function STRTOK()

You are probably better off using strtol or strtoul here instead of strtok
and then trying to do the parsing yourself. For instance:

long l1, l2;
char *endptr;
char *s = "3.12";

l1 = strtol(s, &endptr, 10);
if ( *endptr ) ++endptr;
l2 = strtol(endptr, &endptr, 10);
/* now l1 and l2 will have 3 & 12 resp. */

Read your compiler documentation or man pages for more info. on
strtol/strtoul.
I wrote a main- and a subfunction like:

main() {
int wl=0, il=0;
char *StrIn;

StrIn = "3.12";
printf("%s \n",StrIn);
str2prec(StrIn, wl, il);
}

/******************************************/
void str2prec(char *str_p, int wl, int iwl){
char str_test1[] ="3.12"; //just for testing
char *str_test2 ="3.12"; //just for testing
char *str_split;

printf ("Splitting string \"%s\" in tokens:\n",str_p);
str_split = strtok (str_p,".");
while (pch != NULL)
{
printf ("%s\n",str_split);
str_split = strtok (NULL, " ,.");
}
}

When I use str_p or str_test2 together with STRTOK() this will cause a
memory-error. With str_test1 it works?! Can someone tell why and how I
can manage this problem.

The problem is that strtok expects a modifiable string when it is first
called, and both str_p and str_test2 happen to be pointers to *constant*
strings. This means you're not allowed to modify them, and therefore
cannot pass them to strtok. However, str_test1 happens to be an array of
characters that can be modified. So, when you call strtok with str_test1
everything works. The solution is to either:
a) Make a copy of constant strings into a local array (possibly dynamically
allocated) and pass the copy to strtok instead.
b) Avoid strtok and use strtol/strtoul instead. Since neither of them
modify the source string, they are safe to use with constant strings.

-nrk.
Anyway I would also be happy to hear of some other solutions for string
parsing...

Thanx for any help!
uli
 
C

CBFalconer

nrk said:
You are probably better off using strtol or strtoul here instead of
strtok and then trying to do the parsing yourself. For instance:

long l1, l2;
char *endptr;
char *s = "3.12";

l1 = strtol(s, &endptr, 10);

l1 = strtoul(s, &endptr, 10);
if ('.' == *endptr) {
l2 = strtoul(endptr + 1, &endptr, 10);
}
 
A

Al Bowers

Ulrich said:
Hi all!
since I'am used to work with matlab for a long time and now have to work
with c/c++, I have again some problems with the usage of strings, pointers
and arrays. So please excuse my basic question:

I want to parse a string like "3.12" to get two integers 3 and 12. I wanted
to use the function STRTOK()
I wrote a main- and a subfunction like:

main() {
int wl=0, il=0;
char *StrIn;

StrIn = "3.12";
printf("%s \n",StrIn);
str2prec(StrIn, wl, il);
}

/******************************************/
void str2prec(char *str_p, int wl, int iwl){
char str_test1[] ="3.12"; //just for testing
char *str_test2 ="3.12"; //just for testing
char *str_split;

printf ("Splitting string \"%s\" in tokens:\n",str_p);
str_split = strtok (str_p,".");
while (pch != NULL)
{
printf ("%s\n",str_split);
str_split = strtok (NULL, " ,.");
}
}

When I use str_p or str_test2 together with STRTOK() this will cause a
memory-error. With str_test1 it works?! Can someone tell why and how I can
manage this problem.
Anyway I would also be happy to hear of some other solutions for string
parsing...

You are trying to modify a string literal. It is explained in the faq
question 1.32
http://www.eskimo.com/~scs/C-faq/q1.32.html

Function strtok will modify its argument. Therefore, you must supply a
point to storage that can be modified. Using strtok with str_test1 is
fine.

Another method you can use is function sscanf.
int i1,i2;
sscanf(str_test2,"%d.%d",&i1,&i2);
And check the return value of this function to make sure that
conversions were made.

Probably the best way to do this safely is use function strtol.
This function offers various ways in which you can validate a
correct parse.

An example of strtol.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>

typedef enum {MEM_ERR = -1, PARSE_ERR, OK} bool;

bool parse2prec(const char *str_p, int *iw, int *iwl);

int main(void)
{
int i1, i2;

if(1 == parse2prec("3.14",&i1,&i2))
printf("i1 = %d\ni2 = %d\n",i1,i2);
else puts("Error in parsing string");
return 0;
}

bool parse2prec(const char *str_p, int *iw, int *iwl)
{
long i1, i2;
char *endp, *s,*s1;
int ret;

if(*str_p == '\0') ret = PARSE_ERR;
if((s = malloc(strlen(str_p)+1)) == NULL) ret = MEM_ERR;
else
{
strcpy(s,str_p);
errno = 0;
i1 = strtol(s,&endp,10);
if(*endp != '.' || errno == ERANGE || i1 > (long)INT_MAX ||
i1 < (long)INT_MIN) ret = PARSE_ERR;
else
{
s1 = ++endp;
i2 = strtol(s1,&endp,10);
if(*endp != '\0' || errno == ERANGE || i2 > (long)INT_MAX ||
i2 < 0 ) ret = PARSE_ERR;
}
}
free(s);
if(ret != PARSE_ERR && ret != MEM_ERR)
{
ret = OK;
*iw = i1;
*iwl = i2;
}
return ret;
}
 
J

j

Ulrich Vollenbruch said:
Hi all!
since I'am used to work with matlab for a long time and now have to work
with c/c++, I have again some problems with the usage of strings, pointers
and arrays. So please excuse my basic question:

I want to parse a string like "3.12" to get two integers 3 and 12. I wanted
to use the function STRTOK()
I wrote a main- and a subfunction like:

main() {
int wl=0, il=0;
char *StrIn;

StrIn = "3.12";
printf("%s \n",StrIn);
str2prec(StrIn, wl, il);
}

/******************************************/
void str2prec(char *str_p, int wl, int iwl){
char str_test1[] ="3.12"; //just for testing
char *str_test2 ="3.12"; //just for testing
char *str_split;

printf ("Splitting string \"%s\" in tokens:\n",str_p);
str_split = strtok (str_p,".");
while (pch != NULL)
{
printf ("%s\n",str_split);
str_split = strtok (NULL, " ,.");
}
}

When I use str_p or str_test2 together with STRTOK() this will cause a
memory-error. With str_test1 it works?! Can someone tell why and how I can
manage this problem.
Anyway I would also be happy to hear of some other solutions for string
parsing...

Thanx for any help!
uli

modify a string literal you cannot.
invoke undefined behaviour it does.

Use strcpy if you wish to copy the data of a string literal
to an array and then modify that array. Or simply
initialize the array with the string literal.
 
D

Default User

Uli (uvb) said:
Dear j & nrk,
thank you for your prompt feedback! It was the perfect answer to get me out
a fix! And now I could found it also in my Stroustrup. Its really hard to
get in c after that long time *g* _uli


Please don't top-post. Your replies belong following properly trimmed
quotes.

Your specific question has been answered, but you should be aware that C
and C++ are different languages. There are other solutions to your
question in C++ that are not topical here. If you are working in C++,
and your reference to Stroustrup indicates that you are, you should post
questions to comp.lang.c++.

Naturally, strtok() is a C standard library function, and present in C++
as well.




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top