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


Groups > comp.lang.c > #99872

Re: [Structure of Array] pointer arithmetic in structures

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: [Structure of Array] pointer arithmetic in structures
Date 2016-12-19 20:06 +0000
Organization A noiseless patient Spider
Message-ID <87h95z7kf2.fsf@bsb.me.uk> (permalink)
References (11 earlier) <93d38915-3e06-4916-8578-0cec387a6990@googlegroups.com> <8760mg6a31.fsf@bsb.me.uk> <6669c766-06eb-49b1-9b69-7a3802e1b673@googlegroups.com> <o39b9l$36j$1@dont-email.me> <4ae5cf7f-028c-47f0-b97b-030775324478@googlegroups.com>

Show all headers | View raw


Dexterr <dejangru@gmail.com> writes:
<snip>
> void readComponentFile()
> {
>   FILE *fp = fopen(FILENAME, "r");

Another plan is not to open any file...

>   char line[100]; 
>   
>   if(fp != NULL)

...so you don't need to check this...

>   {
>     while (fgets(line, sizeof line, fp))

...and leave stdin in place of fp.

>     { 
>      int num, den; 
>            
>     if (sscanf(line, " (%d/%d)", &num, &den) == 2)
>     {
>       printf("%g\n", (double)num/den);
>     } 
>     else
>     {
>       puts("Input line should have the form \"(A/B)\"");
>     }
>   }
>   fclose(fp);
>  }
> } 
>
>
> ----
>
> This is what the compiler said:
>
> MacBook-Pro-od-Dejan:NED putolov$ gcc -Wall -o barry barry.c 
> MacBook-Pro-od-Dejan:NED putolov$ ./barry

Then, to run the program, you redirect the input from the file you want
the program to read like this:

 $ ./barry <netlist.txt

I do this because I find it much easier to run lots of test cases.  I
will end up with files called test1.txt, test2.txt etc. with examples of
where some version of the program has gone wrong during development.
With every edit, I run

 $ ./barry <test1.txt
 ...
 $ ./barry <test2.txt

and so on to check they all still work.  I also like the advantage of
being able to type a few test lines by hand.

(Actually I do something just a little more complicated enabling me to
read either a named file or from stdin but that's not going to help you
much right now.)

Of course, I your teacher requires a program that reads just one
pre-decided named file, then you have to do that, but I still make the
the last version I wrote.

-- 
Ben.

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


Thread

[Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 07:51 -0800
  Re: [Structure of Array] pointer arithmetic in structures fir <profesor.fir@gmail.com> - 2016-12-18 08:15 -0800
    Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 09:31 -0800
  Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 16:27 +0000
    Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 09:43 -0800
      Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 18:13 +0000
        Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 10:31 -0800
      Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-18 10:39 -0800
  Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 10:02 -0800
    Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-18 10:20 -0800
      Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 10:40 -0800
        Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-18 10:54 -0800
          Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:10 -0800
        Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 18:54 +0000
          Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:05 -0800
          Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:22 -0800
            Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 19:38 +0000
              Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:45 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-18 20:07 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 12:35 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-18 21:26 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 13:47 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-19 00:23 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 10:53 -0800
                Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-19 19:11 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 11:20 -0800
                Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-19 19:31 +0000
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-19 20:06 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 11:27 -0800
                Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-19 19:40 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 12:09 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-19 20:15 +0000
                Re: [Structure of Array] pointer arithmetic in structures "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> - 2016-12-24 17:12 +1100
                Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-19 16:15 -0800

csiph-web