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_spl it);
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
--
***
For reply remove NOSPAM_ from email-adress
*** 6 2120
"Ulrich Vollenbruch" <NO*********@ic h-will-net.de> wrote in message
news:c1******** **@newssrv.muc. infineon.com... 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_spl it); 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. -- *** For reply remove NOSPAM_ from email-adress ***
Ulrich Vollenbruch wrote: 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_spl it); 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
--
Remove devnull for email
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" <ra*********@de vnull.verizon.n et> wrote in message
news:MJ******** *********@nwrdd c02.gnilink.net ... Ulrich Vollenbruch wrote:
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_spl it); 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 -- Remove devnull for email
nrk wrote: Ulrich Vollenbruch wrote:
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);
l1 = strtoul(s, &endptr, 10);
if ('.' == *endptr) {
l2 = strtoul(endptr + 1, &endptr, 10);
}
if ( *endptr ) ++endptr; l2 = strtol(endptr, &endptr, 10); /* now l1 and l2 will have 3 & 12 resp. */
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Ulrich Vollenbruch wrote: 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_spl it); 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_test 2,"%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(cons t char *str_p, int *iw, int *iwl);
int main(void)
{
int i1, i2;
if(1 == parse2prec("3.1 4",&i1,&i2))
printf("i1 = %d\ni2 = %d\n",i1,i2);
else puts("Error in parsing string");
return 0;
}
bool parse2prec(cons t 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(s tr_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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email) http://www.geocities.com/abowers822/
"Uli (uvb)" wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gerrit Holl |
last post by:
Posted with permission from the author.
I have some comments on this PEP, see the (coming) followup to this message.
PEP: 321
Title: Date/Time Parsing and Formatting
Version: $Revision: 1.3 $
Last-Modified: $Date: 2003/10/28 19:48:44 $
Author: A.M. Kuchling <amk@amk.ca>
Status: Draft
Type: Standards Track
|
by: emerth |
last post by:
Suppose I have these 4 typedefs, where each of the 2nd parameter in
each is a simple class that just contains a different number of strings:
typedef map < char*, cctrlrec_data, ltstr > ctrlrec_table2;
typedef map < char*, ctyperec_data, ltstr > typerec_table2;
typedef map < char*, chotrec_data, ltstr > hotrec_table2;
typedef map < char*, cmtxtrec_data, ltstr > mtxtrec_table2;
I have a stream of XML data coming across a socket and I...
|
by: Christopher Benson-Manica |
last post by:
(if this is a FAQ, I apologize for not finding it)
I have a C-style string that I'd like to cleanly separate into tokens
(based on the '.' character) and then convert those tokens to unsigned
integers. What is the best standard(!) C++ way to accomplish this?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
|
by: ralphNOSPAM |
last post by:
Is there a function or otherwise some way to pull out the target text
within an XML tag?
For example, in the XML tag below, I want to pull out 'CALIFORNIA'.
<txtNameUSState>CALIFORNIA</txtNameUSState>
|
by: Simone Mehta |
last post by:
hi All,
I am parsing a CSV file.
I want to read every row into a char array of reasonable size and then
extract strings from it.
<snippet>
char foo="hello,world,bye,bye,world";
.....
sscanf(foo,"%s%*%s%*%s%*%s%*%s",s1,s2,s3,s4,s5);
<snippet/>
This is giving me junk .
| |
by: neverstill |
last post by:
hi-
I have <a> tags in my DataList. For the href property, I want to do
something like this:
<a href='~/Default.aspx?show=Support&disp=Faq&p=<%#
DataBinder.Eval(Container.DataItem, "name")%>' runat=server>some link</a>
Looks pretty straight forward, but when I add the runat=server property it
stops parsing the <% %> block correctly.
|
by: Lucas Tam |
last post by:
Hi all,
Does anyone know of a GOOD example on parsing text with text qualifiers?
I am hoping to parse text with variable length delimiters/qualifiers. Also,
qualified text could run onto mulitple lines and contain characters like
vbcrlf (thus the multiple lines).
Anyhow, any help would be appreciated. Thanks!
|
by: Dennis Benzinger |
last post by:
Hi!
The following program in an UTF-8 encoded file:
# -*- coding: UTF-8 -*-
FIELDS = ("Fächer", )
FROZEN_FIELDS = frozenset(FIELDS)
FIELDS_SET = set(FIELDS)
|
by: Julian |
last post by:
Hi,
I am having problems with a function that I have been using in my program to
read sentences from a 'command file' and parse them into commands. the
surprising thing is that the program works fine on some computers and not so
fine on others. I tried debugging and cannot make any sense of it. I
narrowed it down to the seekg function and made this simple program which
(from what I understand) does not seem to be working as expected in all...
|
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...
|
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...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |