 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
WhiteFeather Guest
|
Posted: Wed Jan 11, 2006 3:27 pm Post subject: Parsing a csv file? |
|
|
Hi all,
I'm trying to parse each line of a comma delimited file into an array.
The file uses quotation marks to contain strings.
I can break each string into an array using "strtok", but, it fails
whenever the comma delimiter is contained within quotation marks.
For example, here's an input line from the csv file:
"this is a phrase",23, "more stufF"
which I break into an array
a[0] = "this is a phrase"
a[1] = 23
a[2] = "more stufF"
My function works okay with that input until the special case of a
comma appearing within a quoted string, eg:
"this is a phrase",23, "more stufF", "John, Bill, and Bob"
which gives me
a[0] = "this is a phrase"
a[1] = 23
a[2] = "more stufF"
a[3] = "John
a[4] = Bill
a[5] = and Bob"
instead of
a[0] = "this is a phrase"
a[1] = 23
a[2] = "more stufF"
a[3] = "John, Bill, and Bob"
which is what I wanted.
Here's how I was trying to do it:
char **split_text(char *string,char *token, int *count)
{
char **retval = NULL;
char *ptr = NULL;
*count = 0;
ptr = strtok(string,token);
while(ptr != NULL)
{
*count = *count + 1;
retval = (char**)realloc(retval,(*count * sizeof(char*)));
retval[*count - 1] = strdup(ptr);
ptr = strtok(NULL,token);
}
return retval;
}
which won't work because the strtok gives me no opportunity to mention
a delimiter AND token. I need something that takes advantage of regexp,
I guess, but hopefully, there's a common or popular routine for this.
Can someone tell me how to do this the efficient way?
Thanks!
|
|
| Back to top |
|
 |
Pete Guest
|
Posted: Thu Jan 12, 2006 12:00 am Post subject: Re: Parsing a csv file? |
|
|
WhiteFeather wrote:
| Quote: | Hi all,
I'm trying to parse each line of a comma delimited file into an array.
The file uses quotation marks to contain strings.
I can break each string into an array using "strtok", but, it fails
whenever the comma delimiter is contained within quotation marks.
For example, here's an input line from the csv file:
"this is a phrase",23, "more stufF"
which I break into an array
a[0] = "this is a phrase"
a[1] = 23
a[2] = "more stufF"
My function works okay with that input until the special case of a
comma appearing within a quoted string, eg:
"this is a phrase",23, "more stufF", "John, Bill, and Bob"
which gives me
a[0] = "this is a phrase"
a[1] = 23
a[2] = "more stufF"
a[3] = "John
a[4] = Bill
a[5] = and Bob"
instead of
a[0] = "this is a phrase"
a[1] = 23
a[2] = "more stufF"
a[3] = "John, Bill, and Bob"
which is what I wanted.
Here's how I was trying to do it:
char **split_text(char *string,char *token, int *count)
{
char **retval = NULL;
char *ptr = NULL;
*count = 0;
ptr = strtok(string,token);
while(ptr != NULL)
{
*count = *count + 1;
retval = (char**)realloc(retval,(*count * sizeof(char*)));
retval[*count - 1] = strdup(ptr);
ptr = strtok(NULL,token);
}
return retval;
}
which won't work because the strtok gives me no opportunity to mention
a delimiter AND token. I need something that takes advantage of regexp,
I guess, but hopefully, there's a common or popular routine for this.
Can someone tell me how to do this the efficient way?
Thanks!
|
Ow, that's the hard way.
If you want to stick to fairly pure C, just parse the thing yourself
scanning the string looking for commas and toggling an in-quotes flag as
needed. But there's an easy way: investigate TStrings::CommaText and its
relatives DelimitedText, Delimiter, and QuoteChar. You can assign a
CSV-style string to the CommaText property and as long as QuoteChar is
set correctly it'll "parse" your string into elements in TStrings::Strings.
-- Pete
|
|
| Back to top |
|
 |
WhiteFeather Guest
|
Posted: Fri Jan 13, 2006 1:00 am Post subject: Re: Parsing a csv file? |
|
|
Pete wrote:
| Quote: | WhiteFeather wrote:
I'm trying to parse each line of a comma delimited file into an array.
The file uses quotation marks to contain strings.
Ow, that's the hard way.
snip
But there's an easy way: investigate TStrings::CommaText
-- Pete
|
DOH!!!
Of course. And not only have I used it a thousand times, I've used it
for this EXACT purpose before, and umm... for some reason forgot all
about it and wasted a week rolling my own in c.
Thanks for your much needed help!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|