473,287 Members | 1,533 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,287 software developers and data experts.

parsing string into an array

I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:

char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}

To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}

This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?
Nov 14 '05 #1
15 3591
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:
printf("output[%d] is %s.\n";i,output[i++]);


If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.
--
'ignorandus (Latin): "deserving not to be known"'
-- Journal of Self-Referentialism
Nov 14 '05 #2
Walter Roberson wrote:
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:

printf("output[%d] is %s.\n";i,output[i++]);

If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.


Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}
Nov 14 '05 #3


John Smith wrote:

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}


The code provided here seems to be ok. What do you
mean by "did not work either".
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
John Smith wrote:
I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:

char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}

To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}

This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?

Smells like a buffer overflow to me.
Nov 14 '05 #5
John Smith wrote:
To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
(snip)
This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?


Are you aware that this will fail if:
* there are more than 5 "tokens"
* one or more of the tokens is (are) longer than 54 characters

That said, there are a couple more things to know:
* strtok() alters the string it works on. So you can't use
it to parse the same string twice unless you have made a copy
of this string before. This may be what happens to you if
your program tries both of your parsing attempts one after
the other on the same string.
* strtok() is one of those "evilish" C std library functions,
because it maintains an internal structure and thus cannot be
used in nested calls. So you must use it with a lot of care,
and it really is safe only in the simplest programs (unless
you really know what you're doing).
Not worth it in my opinion, since it's so easy to implement
"by hand" in a safe manner.

As a side note, I will add that outside of textbooks, multidimensional
arrays in C are not particularly useful. Their syntax is often
misleading (in my opinion) and they can serve only very limited
purpose. I tend to prefer arrays of arrays, which are easier to handle
and more flexible (in my opinion).
Nov 14 '05 #6
John Smith wrote:
Walter Roberson wrote:
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:
printf("output[%d] is %s.\n";i,output[i++]);

If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.

Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}


Hi John,
(Do us a favor. See if you can turn off the annoying MIME stuff)
Like Al, I can't see what "did not work". Of course, you have not shown
us what the original 'char *str = "...";' looks like, or where you get
it. Enquiring Minds want to know.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #7
Joe Wright wrote:
John Smith wrote:
Walter Roberson wrote:
John Smith <js****@company.com> wrote:

printf("output[%d] is %s.\n";i,output[i++]);

If I recall correctly, this uses undefined behaviour. .... snip ...
It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.
Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

.... snip ...
(Do us a favor. See if you can turn off the annoying MIME stuff)

.... snip ...

I am among the first to bitch and moan about mime and html in
Usenet, but I see none in this thread. Has something stripped it
on the path from there to here?

--
"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 #8
Thanks to those who replied to my queston. I appreciate your help.
The code did work. I wasn't aware strtok would change the original
string. That caused some confusion.

(Is MIME turned off this time? Hope it is.)
Nov 14 '05 #9
John Smith wrote:
Thanks to those who replied to my queston. I appreciate your help.
The code did work. I wasn't aware strtok would change the original
string. That caused some confusion.

(Is MIME turned off this time? Hope it is.)


I can't tell about MIME anymore. After I wrote my note to you I've set
Thunderbird to ignore it.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10
CBFalconer wrote:
Joe Wright wrote:

... snip ...
(Do us a favor. See if you can turn off the annoying MIME stuff)


... snip ...

I am among the first to bitch and moan about mime and html in
Usenet, but I see none in this thread. Has something stripped it
on the path from there to here?


I suspect you have turned it off in your reader, as I now have.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #11
In article <42***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
I am among the first to bitch and moan about mime and html in
Usenet, but I see none in this thread. Has something stripped it
on the path from there to here?


It isn't the typical obvious MIME so my reader (trn) didn't notice
it either. He did, though, have a Content-type: text/plain
header that specified a charset of Big5. Content-Type is a MIME
header, and on some systems the Big5 (i.e., one of the Chinese
encodings) would trigger different behaviour than if it were
(say) 8859-1 .
--
Studies show that the average reader ignores 106% of all statistics
they see in .signatures.
Nov 14 '05 #12
Guillaume <"grsNOSPAM at NOTTHATmail dot com"> writes:
John Smith wrote:
To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
(snip)
This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?
[snip] As a side note, I will add that outside of textbooks, multidimensional
arrays in C are not particularly useful. Their syntax is often
misleading (in my opinion) and they can serve only very limited
purpose. I tend to prefer arrays of arrays, which are easier to handle
and more flexible (in my opinion).


I'm not certain what distinction you're making here. The declaration
char output[5][55];
defines output as an array of arrays of char, which is the closest
thing C has to a multi-dimensional array.

Perhaps you're thinking of an array of pointers to arrays?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
John Smith <js****@company.com> spoke thus:
(Is MIME turned off this time? Hope it is.)


It seems not to be - the header that Walter referred to is still
present:

Content-Type: text/plain; charset=Big5

although it also looks like the people it annoys have been able to
change their newsreader configuration, so you may not have to worry
about it (tin has not complained about it here, FWIW).

--
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.
Nov 14 '05 #14
Christopher Benson-Manica wrote:
John Smith <js****@company.com> spoke thus:
(Is MIME turned off this time? Hope it is.)


It seems not to be - the header that Walter referred to is still
present:

Content-Type: text/plain; charset=Big5

although it also looks like the people it annoys have been able to
change their newsreader configuration, so you may not have to worry
about it (tin has not complained about it here, FWIW).


There is no attached mime section, nor any html. So I see no harm
in it, although I am one of the most vocal html/mime haters
extant.

--
"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 #15

Christopher Benson-Manica wrote:
John Smith <js****@company.com> spoke thus:
(Is MIME turned off this time? Hope it is.)


It seems not to be - the header that Walter referred to is still
present:

Content-Type: text/plain; charset=Big5

although it also looks like the people it annoys have been able to
change their newsreader configuration, so you may not have to worry
about it (tin has not complained about it here, FWIW).

All it does for me is change the font from the default to a sans-serif
one. It's annoying to a certain extent, but not a killer. I haven't
figured out how to make XanaNews ignore it, if even possible.

Brian

Nov 14 '05 #16

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

Similar topics

4
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'. ...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
6
by: Ulrich Vollenbruch | last post by:
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...
1
by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
12
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawk¹ parsing an XHTML file containing a hCard², without luck. I wonder if you guys...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
2
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important...
1
nine72
by: nine72 | last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance… I will try my best to describe what I have going on… 1) I have 15...
9
nine72
by: nine72 | last post by:
Ok, so I have figured out how to parse an custom returned XML doc (actually a string that I made into a doc for testing). At this point I am attempting to integrate my parse routine into my main...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.