Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #76241

Re: Please help -- cannot read double data from file into array

From BartC <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Please help -- cannot read double data from file into array
Date 2015-11-17 14:20 +0000
Organization A noiseless patient Spider
Message-ID <n2fcvj$d6p$1@dont-email.me> (permalink)
References <bb0cef7b-950a-4fcb-a7c9-2a8fbe558cd7@googlegroups.com> <n2f580$g5i$1@dont-email.me> <87si44zre2.fsf@bsb.me.uk>

Show all headers | View raw


On 17/11/2015 13:28, Ben Bacarisse wrote:
> BartC <bc@freeuk.com> writes:

>> void parsestring(char *cp, double a[][2])
>> {
>> 	a[0][0]=10.0;
>> 	a[0][1]=20.0;
>> 	a[1][0]=110.0;
>> 	a[1][1]=120.0;
>> 	a[2][0]=210.0;
>> 	a[2][1]=220.0;
>> 	a[3][0]=310.0;
>> 	a[3][1]=320.0;
>> }
>
> To the OP: I think BartC is just checking that the parameters types are
> correct here.  Don't that this sketch as the right way to do it since it
> removes your line-by-line loop and could be seen as suggesting that all
> the data should be read into a single string first!

Yes, I got the idea that there was a problem with the way the array was 
passed.

But I couldn't make the OP's program work. And I couldn't make head or 
tail of what strtok() was supposed to do; even after reading the docs I 
couldn't make it work.

So I created an alternative parsestring(), which uses your idea of 
taking the row number from the input:

void parsestring(char *cp, double a[][2])
{
char cptr[100];
double temp = 0;
unsigned int row = 0;
unsigned int col = 0;

if(cp != NULL)
{
    nexttoken(&cp,cptr,sizeof(cptr));
    row = atol(cptr);
    if (row>=NUMROWS) return;

    while (nexttoken(&cp,cptr,sizeof(cptr)) && col<2) {
       temp = strtod(cptr, NULL);
       a[row][col++]=temp;
    }
}
}

This uses an alternative to strtok that I know works! It's here (these 
functions will need forward declarations, or rearranged to be in the 
correct order):

int nexttoken(char **ptr,char *dest,int destsize)
{
char *p;
int n;
char c;
     p=*ptr;
     while (c=*p,c==' ' || c=='\t') ++p;

     if (c==0)
     {
         *ptr=p;
         *dest=0;
         return 0;
     }
     n=0;
     do
     {
         if (n<destsize)
         {
             *dest=c;
             ++dest;
             ++n;
         }
         ++p;
         c=*p;
     } while (!(c==0 || c==' ' || c=='\t'));
     *ptr=p;
     *dest=0;
     return n;
}

(This only allows space and tab separators.)

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Please help -- cannot read double data from file into array dakupoto@gmail.com - 2015-11-17 00:39 -0800
  Re: Please help -- cannot read double data from file into array Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-17 00:54 -0800
  Re: Please help -- cannot read double data from file into array gamo <gamo@telecable.es> - 2015-11-17 11:36 +0100
    Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 11:04 +0000
  Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 11:02 +0000
  Re: Please help -- cannot read double data from file into array James Kuyper <jameskuyper@verizon.net> - 2015-11-17 06:51 -0500
  Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-17 12:08 +0000
    Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 13:28 +0000
      Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-17 14:20 +0000
        Re: Please help -- cannot read double data from file into array dakupoto@gmail.com - 2015-11-17 20:50 -0800
          Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-18 11:22 +0000
            Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-18 12:15 +0000
            Re: Please help -- cannot read double data from file into array dakupoto@gmail.com - 2015-11-18 23:07 -0800
              Re: Please help -- cannot read double data from file into array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-19 10:41 +0000
              Re: Please help -- cannot read double data from file into array BartC <bc@freeuk.com> - 2015-11-19 11:13 +0000

csiph-web