Google Groups Home
Help | Sign in
What am I doing wrong?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 50 - Collapse all   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Cromulent  
View profile
 More options Jul 5, 9:22 pm
Newsgroups: comp.lang.c
From: Cromulent <cromul...@justextrememetal.com>
Date: Sun, 6 Jul 2008 02:22:41 +0100
Local: Sat, Jul 5 2008 9:22 pm
Subject: What am I doing wrong?
I think I know what the problem is. I'm passing an array to strdup()
which is not what it wants and is producing a bus error. But, even
knowing the cause of the problem I'm having difficulties thinking of a
suitable solution.

I'm basically reading a CSV file and then tokenising it to get the
relevant values in a useable form. So I need to read the file one line
at a time in order to tokenise each one and put the values in the
correct place but I can't think of a way off the top of my head to read
a single line at a time using a function which would put it in a
malloced char*.

Here is the code.

int tokeniseInput(char readstring[])
{
        char *cp;
        const char delimiters[] = " ,'\n'";

        proc = readstring;

        smStrings *stockmarket;

        cp = strdup(readstring);

        stockmarket->smsdate = strtok(cp, delimiters);
        printf("process1 = %s\n", stockmarket->smsdate);
        stockmarket->smsopen = strtok(NULL, delimiters);
        printf("process2 = %s\n", stockmarket->smsopen);
        stockmarket->smshigh = strtok(NULL, delimiters);
        printf("process3 = %s\n", stockmarket->smshigh);
        stockmarket->smslow = strtok(NULL, delimiters);
        printf("process4 = %s\n", stockmarket->smslow);
        stockmarket->smsclose = strtok(NULL, delimiters);
        printf("process5 = %s\n", stockmarket->smsclose);
        stockmarket->smsvolume = strtok(NULL, delimiters);
        printf("process6 = %s\n", stockmarket->smsvolume);

        return 0;

}

--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cromulent  
View profile
 More options Jul 5, 9:25 pm
Newsgroups: comp.lang.c
From: Cromulent <cromul...@justextrememetal.com>
Date: Sun, 6 Jul 2008 02:25:09 +0100
Local: Sat, Jul 5 2008 9:25 pm
Subject: Re: What am I doing wrong?
On 2008-07-06 02:22:41 +0100, Cromulent <cromul...@justextrememetal.com> said:

>    proc = readstring;

Bah, ignore this bit. I deleted it from my code after I copied it to my
post and forgot to delete it.
--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile
 More options Jul 5, 10:16 pm
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Sun, 06 Jul 2008 03:16:23 +0100
Local: Sat, Jul 5 2008 10:16 pm
Subject: Re: What am I doing wrong?

Cromulent <cromul...@justextrememetal.com> writes:
> I think I know what the problem is. I'm passing an array to strdup()

No you are not despite appearances.

> which is not what it wants and is producing a bus error. But, even
> knowing the cause of the problem I'm having difficulties thinking of a
> suitable solution.

> I'm basically reading a CSV file and then tokenising it to get the
> relevant values in a useable form. So I need to read the file one line
> at a time in order to tokenise each one and put the values in the
> correct place but I can't think of a way off the top of my head to
> read a single line at a time using a function which would put it in a
> malloced char*.

You don't need to read one line at a time.  In fact that can make the
parsing of a CSV file harder (or at least, it can fail to make it
simple).

> Here is the code.

> int tokeniseInput(char readstring[])

readstring is a pointer despite the [].  C can't pass arrays and
rather than forbid this syntax it was taken to mean the same as char
*readstring in the context.

> {
>    char *cp;
>    const char delimiters[] = " ,'\n'";

Odd.  You know you have two 's in there?  ' is not even special in CSV
files as far as I know.

>    proc = readstring;

OK, ignoring this as per you other message.

>    smStrings *stockmarket;

Not pointing anywhere.  Using it with -> will cause undefined behaviour.

>    cp = strdup(readstring);

You will get a memory leak if you don't record the cp pointer
somewhere (it won't always be returned by the first call to strtok).
Without it you can't free the memory, ever.

>    stockmarket->smsdate = strtok(cp, delimiters);

Bang! stockmarket->smsdate is undefined unless stockmarket has been
made to point to the right sort of thing.

>    printf("process1 = %s\n", stockmarket->smsdate);
>    stockmarket->smsopen = strtok(NULL, delimiters);
>    printf("process2 = %s\n", stockmarket->smsopen);
>    stockmarket->smshigh = strtok(NULL, delimiters);
>    printf("process3 = %s\n", stockmarket->smshigh);
>    stockmarket->smslow = strtok(NULL, delimiters);
>    printf("process4 = %s\n", stockmarket->smslow);
>    stockmarket->smsclose = strtok(NULL, delimiters);
>    printf("process5 = %s\n", stockmarket->smsclose);
>    stockmarket->smsvolume = strtok(NULL, delimiters);
>    printf("process6 = %s\n", stockmarket->smsvolume);

This will only work if your input is very simple.  The slightest
unexpected thing like an empty field or a comma in a field and it all
goes haywire.

>    return 0;
> }

--
Ben.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cromulent  
View profile
 More options Jul 5, 10:49 pm
Newsgroups: comp.lang.c
From: Cromulent <cromul...@justextrememetal.com>
Date: Sun, 6 Jul 2008 03:49:04 +0100
Local: Sat, Jul 5 2008 10:49 pm
Subject: Re: What am I doing wrong?
On 2008-07-06 03:16:23 +0100, Ben Bacarisse <ben.use...@bsb.me.uk> said:

> Cromulent <cromul...@justextrememetal.com> writes:

>> Here is the code.

>> int tokeniseInput(char readstring[])

> readstring is a pointer despite the [].  C can't pass arrays and
> rather than forbid this syntax it was taken to mean the same as char
> *readstring in the context.

Ah, I see thanks.

>> {
>>        char *cp;
>>        const char delimiters[] = " ,'\n'";

> Odd.  You know you have two 's in there?  ' is not even special in CSV
> files as far as I know.

Ah I meant it to be '\n' not ' and \n if that makes sense. They were
meant to be enclosing the newline character as I was not sure if it was
needed or not. I guess not.

>>        smStrings *stockmarket;

> Not pointing anywhere.  Using it with -> will cause undefined behaviour.

How do you mean? It points to a typedefed struct which is declared in
the header file.

>>        cp = strdup(readstring);

> You will get a memory leak if you don't record the cp pointer
> somewhere (it won't always be returned by the first call to strtok).
> Without it you can't free the memory, ever.

Ah. Good catch, I need to fix that up then.

It is just stock market data downloaded from Google Finance. No real
chance of any funny input. But I see your point.

--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Heathfield  
View profile
 More options Jul 5, 11:11 pm
Newsgroups: comp.lang.c
From: Richard Heathfield <r...@see.sig.invalid>
Date: Sun, 06 Jul 2008 03:11:44 +0000
Local: Sat, Jul 5 2008 11:11 pm
Subject: Re: What am I doing wrong?
Ben Bacarisse said:

> Cromulent <cromul...@justextrememetal.com> writes:

<snip>

>> int tokeniseInput(char readstring[])

> readstring is a pointer despite the [].  C can't pass arrays and
> rather than forbid this syntax it was taken to mean the same as char
> *readstring in the context.

No, that wasn't the reason, as a matter of fact. In the early days of C,
pointers were described using [] rather than *. It wasn't long before the
* was introduced, at which point [] was used exclusively for arrays,
*except* in this situation - i.e. a function receiving a pointer as a
parameter. Just why it was retained, I don't know - possibly a nod in the
direction of backwards compatibility.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile
 More options Jul 5, 11:09 pm
Newsgroups: comp.lang.c
From: Ian Collins <ian-n...@hotmail.com>
Date: Sun, 06 Jul 2008 15:09:07 +1200
Local: Sat, Jul 5 2008 11:09 pm
Subject: Re: What am I doing wrong?
Cromulent wrote:
> On 2008-07-06 03:16:23 +0100, Ben Bacarisse <ben.use...@bsb.me.uk> said:

>> Cromulent <cromul...@justextrememetal.com> writes:

>>>     smStrings *stockmarket;

>> Not pointing anywhere.  Using it with -> will cause undefined behaviour.

> How do you mean? It points to a typedefed struct which is declared in
> the header file.

Not in the code you posted it doesn't.  You just declare the pointer,
you don't assign any value to it.

--
Ian Collins.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cromulent  
View profile
 More options Jul 5, 11:33 pm
Newsgroups: comp.lang.c
From: Cromulent <cromul...@justextrememetal.com>
Date: Sun, 6 Jul 2008 04:33:00 +0100
Subject: Re: What am I doing wrong?
On 2008-07-06 04:09:07 +0100, Ian Collins <ian-n...@hotmail.com> said:

> Cromulent wrote:
>> On 2008-07-06 03:16:23 +0100, Ben Bacarisse <ben.use...@bsb.me.uk> said:

>>> Cromulent <cromul...@justextrememetal.com> writes:

>>>> smStrings *stockmarket;

>>> Not pointing anywhere.  Using it with -> will cause undefined behaviour.

>> How do you mean? It points to a typedefed struct which is declared in
>> the header file.

> Not in the code you posted it doesn't.  You just declare the pointer,
> you don't assign any value to it.

Yes, I see now. 4.30am here and I'm a tad tired :).

So I've changed it to the following:

smStrings *stockStrings = malloc(sizeof(smStrings));
--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile
 More options Jul 5, 11:39 pm
Newsgroups: comp.lang.c
From: Ian Collins <ian-n...@hotmail.com>
Date: Sun, 06 Jul 2008 15:39:36 +1200
Local: Sat, Jul 5 2008 11:39 pm
Subject: Re: What am I doing wrong?

Every hour spent coding while tired adds two to the job fixing the bugs!

--
Ian Collins.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Schwarz  
View profile
 More options Jul 6, 12:41 am
Newsgroups: comp.lang.c
From: Barry Schwarz <schwa...@dqel.com>
Date: Sat, 05 Jul 2008 21:41:52 -0700
Local: Sun, Jul 6 2008 12:41 am
Subject: Re: What am I doing wrong?
On Sun, 6 Jul 2008 02:22:41 +0100, Cromulent

<cromul...@justextrememetal.com> wrote:
>I think I know what the problem is. I'm passing an array to strdup()
>which is not what it wants and is producing a bus error. But, even
>knowing the cause of the problem I'm having difficulties thinking of a
>suitable solution.

The real error is not in the code you posted.  You need to provide a
complete compilable program that demonstrates the undesired behavior.

>I'm basically reading a CSV file and then tokenising it to get the
>relevant values in a useable form. So I need to read the file one line
>at a time in order to tokenise each one and put the values in the
>correct place but I can't think of a way off the top of my head to read
>a single line at a time using a function which would put it in a
>malloced char*.

Look up fgets in your reference.

Remove del for email


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark McIntyre  
View profile
 More options Jul 6, 5:55 am
Newsgroups: comp.lang.c
From: Mark McIntyre <markmcint...@TROUSERSspamcop.net>
Date: Sun, 06 Jul 2008 10:55:45 +0100
Local: Sun, Jul 6 2008 5:55 am
Subject: Re: What am I doing wrong?

Cromulent wrote:
> ??     smStrings *stockmarket;

>> Not pointing anywhere.  Using it with -> will cause undefined behaviour.

> How do you mean? It points to a typedefed struct which is declared in
> the header file.

stockmarket is a pointer. But it isn't pointing AT anything.
You need to allocate some memory for the object.

>> This will only work if your input is very simple.  The slightest
>> unexpected thing like an empty field or a comma in a field and it all
>> goes haywire.

> It is just stock market data downloaded from Google Finance. No real
> chance of any funny input. But I see your point.

  :-) I like your optimism in the accuracy of google!

You may want to consider what happens when they add/delete/change a
column however.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cromulent  
View profile
 More options Jul 6, 7:33 am
Newsgroups: comp.lang.c
From: Cromulent <cromul...@justextrememetal.com>
Date: Sun, 6 Jul 2008 12:33:24 +0100
Local: Sun, Jul 6 2008 7:33 am
Subject: Re: What am I doing wrong?
On 2008-07-06 05:41:52 +0100, Barry Schwarz <schwa...@dqel.com> said:

>> I'm basically reading a CSV file and then tokenising it to get the
>> relevant values in a useable form. So I need to read the file one line
>> at a time in order to tokenise each one and put the values in the
>> correct place but I can't think of a way off the top of my head to read
>> a single line at a time using a function which would put it in a
>> malloced char*.

> Look up fgets in your reference.

That is what I use already.
--
"I disapprove of what you say, but I'll defend to the death your right
to say it." - Voltaire

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
santosh  
View profile
 More options Jul 6, 8:38 am
Newsgroups: comp.lang.c
From: santosh <santosh....@gmail.com>
Date: Sun, 06 Jul 2008 18:08:31 +0530
Local: Sun, Jul 6 2008 8:38 am
Subject: Re: What am I doing wrong?

Cromulent wrote:
> On 2008-07-06 05:41:52 +0100, Barry Schwarz <schwa