help needed with filnames as command line arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CBFalconer

    #16
    Style and Decompostion (was Re: help needed with filnames as commandline arguments)

    Simon Biber wrote (minisculy edited):[color=blue]
    >[/color]
    .... snip ...[color=blue]
    >
    > #include <stdio.h>
    > int main(int argc, char **argv)
    > {
    > int i;
    > for (i = 1; i < argc; i++) {
    > int value = 0;
    > FILE *fp = fopen(argv[i], "r");
    > if (fp) {
    > if (fscanf(fp, "%d", &value) == 1)) {
    > printf("%s: %d\n", argv[i], value);
    > }
    > fclose(fp);
    > }
    > }
    > return 0;
    > }[/color]

    While C has, for some time, allowed this sort of declaration
    within a block, I see very little use for it. The code is much
    more flexible if broken into a separate routine. The only cost is
    (possibly) the time to call and return, which would be vanishingly
    small here compared with the actual routine action. Thus I would
    recommend:

    #include <stdio.h>
    static void tryfile(const char *fn)
    {
    int value;
    FILE *fp;
    if (fp = fopen(fn, "r"))
    if (fscanf(fp, "%d", &value) == 1) {
    printf("%s: %d\n", fn, value);
    }
    fclose(fp);
    }
    }

    int main(int argc, char **argv)
    {
    int i;
    for (i = 1; i < argc; i++) {
    tryfile(argv[i]);
    }
    return 0;
    }

    which, at least to me, is clearer in that each routine has one
    simple responsibility. With C99 you can also have all the
    benefits of the single routine method by using the inline
    directive. YMMV.

    With this organization it is easier (to me) to see that one should
    worry about the action of fopen on an empty string, and trivial to
    avoid it. We can also easily see that initialization during
    declaration is not needed anywhere.

    There is no need to agree with me :-) but at least you can
    evaluate my preference after seeing the reasons.

    --
    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home .att.net> USE worldnet address!

    Comment

    • Irrwahn Grausewitz

      #17
      Re: Style and Decompostion (was Re: help needed with filnames as command line arguments)

      CBFalconer <cbfalconer@yah oo.com> wrote:
      [color=blue]
      >Simon Biber wrote (minisculy edited):[color=green]
      >>[/color]
      >... snip ...[color=green]
      >>
      >> #include <stdio.h>
      >> int main(int argc, char **argv)
      >> {
      >> int i;
      >> for (i = 1; i < argc; i++) {
      >> int value = 0;
      >> FILE *fp = fopen(argv[i], "r");
      >> if (fp) {
      >> if (fscanf(fp, "%d", &value) == 1)) {
      >> printf("%s: %d\n", argv[i], value);
      >> }
      >> fclose(fp);
      >> }
      >> }
      >> return 0;
      >> }[/color]
      >
      >While C has, for some time, allowed this sort of declaration
      >within a block, I see very little use for it.[/color]

      Alas, I sometimes use initialization within a block in switch
      statements, e.g. where a helper variable is needed in only one
      special case and I want to have it declared where it's used:

      /*...*/
      int foo, bar;

      switch( whatever )
      {
      case CASE_X:
      /* do something */
      break;

      case CASE_SWAP:
      {
      int tmp = foo;
      foo = bar;
      bar = tmp;
      }
      break;

      case CASE_Y:
      /* do something else */
      break;

      default:
      /* perform default action */
      }


      Regards
      --
      Irrwahn
      (irrwahn33@free net.de)

      Comment

      • CBFalconer

        #18
        Re: Style and Decompostion (was Re: help needed with filnames ascommand line arguments)

        Irrwahn Grausewitz wrote:[color=blue]
        > CBFalconer <cbfalconer@yah oo.com> wrote:
        >[/color]
        .... snip ...[color=blue][color=green]
        > >
        > > While C has, for some time, allowed this sort of declaration
        > > within a block, I see very little use for it.[/color]
        >
        > Alas, I sometimes use initialization within a block in switch
        > statements, e.g. where a helper variable is needed in only one
        > special case and I want to have it declared where it's used:[/color]

        You snipped my point, which was that such usage often indicates
        the need to repartition the code.

        --
        Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net> USE worldnet address!


        Comment

        • Irrwahn Grausewitz

          #19
          Re: Style and Decompostion (was Re: help needed with filnames as command line arguments)

          CBFalconer <cbfalconer@yah oo.com> wrote:
          [color=blue]
          >Irrwahn Grausewitz wrote:[color=green]
          >> CBFalconer <cbfalconer@yah oo.com> wrote:
          >>[/color]
          >... snip ...[color=green][color=darkred]
          >> >
          >> > While C has, for some time, allowed this sort of declaration
          >> > within a block, I see very little use for it.[/color]
          >>
          >> Alas, I sometimes use initialization within a block in switch
          >> statements, e.g. where a helper variable is needed in only one
          >> special case and I want to have it declared where it's used:[/color]
          >
          >You snipped my point, which was that such usage often indicates
          >the need to repartition the code.[/color]

          I snipped it because your point remains valid and I support it, but in
          special cases I ignore it, when I don't want to repartition the code.

          Admittedly my actual example was a rather silly one. A more realistic
          example would've been a message handler routine, but I didn't want to
          clutter my post with ugly OT windowish code.

          Regards
          --
          Irrwahn
          (irrwahn33@free net.de)

          Comment

          • Craigb

            #20
            Re: help needed with filnames as command line arguments

            > The second argument of printargstuff() has to be a[color=blue]
            > pointer-to-pointer-to-character. You pass *argc[0] to it,
            > which is a character. Now please explain how this is
            > supposed to work without invoking undefined behaviour.
            >
            >
            > [1] You most probably didn't set your warning level to a
            > suitable amount. Warnings usually _are_ errors!
            >
            > Regards[/color]

            There you go, now I learned something!..
            I re-did it in the ide and of course found what you are saying. I
            won't go too far off topic, but the reason I was in the threads at all
            was to find out exactly that sort of thing, so no-bad from my point of
            view. Of course now I'll be doing searches on your login to find out
            what other tips I can glean.
            /cb.

            Comment

            • Irrwahn Grausewitz

              #21
              Re: help needed with filnames as command line arguments

              craigb@smartcha t.net.au (Craigb) wrote:

              <snip>[color=blue]
              >Of course now I'll be doing searches on your login to find out
              >what other tips I can glean.[/color]

              Er, be careful, I'm not an expert, and thus my posts are often an
              example for how to /not/ do things. You'd better do a search for
              posts originating from regulars like (in no particular order):

              Chris Torek
              Dan Pop
              Douglas A. Gwyn
              Jack Klein
              Richard Heathfield
              ...
              <list incomplete>

              Regards
              --
              Irrwahn
              (irrwahn33@free net.de)

              Comment

              • Floyd Davidson

                #22
                Re: help needed with filnames as command line arguments

                Irrwahn Grausewitz <irrwahn33@free net.de> wrote:[color=blue]
                >craigb@smartch at.net.au (Craigb) wrote:
                >
                ><snip>[color=green]
                >>Of course now I'll be doing searches on your login to find out
                >>what other tips I can glean.[/color]
                >
                >Er, be careful, I'm not an expert, and thus my posts are often an
                >example for how to /not/ do things. You'd better do a search for
                >posts originating from regulars like (in no particular order):
                >
                > Chris Torek
                > Dan Pop
                > Douglas A. Gwyn
                > Jack Klein
                > Richard Heathfield
                > ...
                > <list incomplete>[/color]

                Lawrence Kirby unfortunately hasn't been posting to clc for over
                a year now, but for relative newbies who want to search the
                archives, /Lawrence/ /Kirby/ stands second only to /Chris/ /Torek/
                in the history of comp.lang.c for having spent _many_ years
                posting readable, tolerant, tutorial articles that are not only
                accurate and detailed, but written in a style that can be read
                and understood by the neophyte as well as the expert.

                I don't mean to suggest that others lack knowledge of C or even
                longevity on c.l.c, but Chris and Larry stand out from the crowd
                when it comes to style and writing ability.

                Hence, along with Steve Summit's FAQ, using google to search for
                articles by "Chris Torek (nospam@elf.eng .bsdi.com)" and/or
                "Lawrence Kirby (fred@genesis.d emon.co.uk)" is probably about
                the most productive learning tool for C available on the web.

                --
                Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
                Ukpeagvik (Barrow, Alaska) floyd@barrow.co m

                Comment

                • Richard Heathfield

                  #23
                  Re: help needed with filnames as command line arguments

                  Floyd Davidson wrote:
                  [color=blue]
                  > Irrwahn Grausewitz <irrwahn33@free net.de> wrote:[/color]
                  <snip>[color=blue][color=green]
                  >>You'd better do a search for
                  >>posts originating from regulars like (in no particular order):
                  >>
                  >> Chris Torek
                  >> Dan Pop
                  >> Douglas A. Gwyn
                  >> Jack Klein
                  >> Richard Heathfield[/color][/color]

                  (cough)

                  Add Ben Pfaff, Dann Corbit, Christian Bau, Joe Maun (not that he's been
                  around for a while either), Steve Summit (ditto, alas), and "those who know
                  me have no need of my name". If my name belongs on the list (which is
                  arguable), all these names most certainly belong on it too.
                  [color=blue][color=green]
                  >> ...
                  >> <list incomplete>[/color][/color]

                  ....even after my additions.
                  [color=blue]
                  > Lawrence Kirby unfortunately hasn't been posting to clc for over
                  > a year now, but for relative newbies who want to search the
                  > archives, /Lawrence/ /Kirby/ stands second only to /Chris/ /Torek/
                  > in the history of comp.lang.c for having spent _many_ years
                  > posting readable, tolerant, tutorial articles that are not only
                  > accurate and detailed, but written in a style that can be read
                  > and understood by the neophyte as well as the expert.[/color]

                  I will cheerfully second that statement. Lawrence Kirby is a byword (all
                  right, make that two bywords) for excellence in comp.lang.c.

                  --
                  Richard Heathfield : binary@eton.pow ernet.co.uk
                  "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
                  C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
                  K&R answers, C books, etc: http://users.powernet.co.uk/eton

                  Comment

                  • Dave Thompson

                    #24
                    Re: help needed with filnames as command line arguments

                    On 20 Oct 2003 03:57:17 -0800, Floyd Davidson <floyd@barrow.c om>
                    wrote:
                    <snip>[color=blue]
                    > Lawrence Kirby unfortunately hasn't been posting to clc for over
                    > a year now, but for relative newbies who want to search the
                    > archives, /Lawrence/ /Kirby/ stands second only to /Chris/ /Torek/
                    > in the history of comp.lang.c for having spent _many_ years
                    > posting readable, tolerant, tutorial articles <snip>
                    > Hence, along with Steve Summit's FAQ, using google to search for
                    > articles by "Chris Torek (nospam@elf.eng .bsdi.com)" and/or
                    > "Lawrence Kirby (fred@genesis.d emon.co.uk)" is probably about
                    > the most productive learning tool for C available on the web.[/color]

                    Don't specify that exact address for Chris, he's had to change
                    (several times, if I recall) so you won't find everything. Without
                    actually trying, I'd guess Torek + comp.lang.c should be enough.

                    - David.Thompson1 at worldnet.att.ne t

                    Comment

                    • Chris Torek

                      #25
                      Re: help needed with filnames as command line arguments

                      In article <87vfqji285.fld @barrow.com>
                      Floyd Davidson <floyd@barrow.c om> writes:[color=blue]
                      >At one time, just prior to C89, there was some serious thought
                      >given to collecting the c.l.c articles of Chris Torek for
                      >publication as a C tutorial. That became a moot when the
                      >Standard was released ...[/color]

                      I must also note that there is an enormous difference between
                      a "collection of vignettes" and a "complete novel", as it were.
                      [color=blue]
                      >However, I wonder if Chris has ever given any thought to the
                      >specifics of his writing style? And if he would be willing to
                      >consider a tutorial for writing technical articles for Usenet?
                      >Or, maybe even just a few words on the subject?[/color]

                      I am not really sure what you mean by the first question. As for
                      the second or third -- well, it would help if I knew how I do it. :-)
                      I think there are a few key points, however, that I could mention.

                      The first is simple enough. If you want to write good code, you
                      should read good code. If you want to write good prose, you should
                      read good prose. If you want to write good tutorials...

                      Obviously, you must know the material. But in this case there is
                      a subtler, yet key, point: you must also know the audience, after
                      a fashion. Here, Usenet has an advantage over books, because the
                      writers interact (to whatever limited extent) with their readers.
                      This means that the writer gets *some* feedback -- at the least,
                      something on the level of "oh I get it" vs "huh?" -- now and then.

                      Finally, there is no substitute for sitting back and thinking.
                      People say that Feynman's method of solving physics problems was:
                      (1) write down the problem; (2) sit and think; (3) write down the
                      answer. Not everyone can be a Feynman, but just about anyone can
                      do the "sit and think" part now and then. I think (after sitting
                      and thinking :-) ) that this is probably the hardest part of the
                      whole process, not just to do but to explain. How is it that we
                      (sentient beings in general) make connections and analogies? How
                      do we generalize? How do we test a generalization once made? (I
                      often resort to the "throw a few examples at it to see if they
                      pass" test, as if testing spaghetti for doneness by whether it
                      sticks to the wall. It works well for a first cut, at least.)

                      Computer programming is, at its heart, about abstraction --
                      controlling complexity by removing irrelevant detail, at the point
                      where it becomes irrelevant. I am not at all sure that tutorial-writing
                      is about abstraction. It seems different, somehow. Certainly
                      there are people who are quite good at one and terrible at the
                      other. But some of the elements are the same: programs can be
                      terribly complex, and often the only way to get them right is to
                      simplify them. This gives rise to program structures, including
                      data structures. Tutorials likewise need structure, so that details
                      appear only where they are relevant. But the structures themselves
                      are different, or at least, have quite different constraints.

                      Well, that seems quite a few words for saying "I don't know", but
                      I am prone to a certain prolixity. :-) There is really only one
                      part that I am sure of: if you want to write, you must read, read,
                      read.
                      --
                      In-Real-Life: Chris Torek, Wind River Systems
                      Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                      email: forget about it http://67.40.109.61/torek/index.html (for the moment)
                      Reading email is like searching for food in the garbage, thanks to spammers.

                      Comment

                      • Floyd Davidson

                        #26
                        Re: help needed with filnames as command line arguments

                        Chris Torek <nospam@elf.eng .bsdi.com> wrote:[color=blue]
                        >In article <87vfqji285.fld @barrow.com>
                        >Floyd Davidson <floyd@barrow.c om> writes:[color=green]
                        >>At one time, just prior to C89, there was some serious thought
                        >>given to collecting the c.l.c articles of Chris Torek for
                        >>publication as a C tutorial. That became a moot when the
                        >>Standard was released ...[/color]
                        >
                        >I must also note that there is an enormous difference between
                        >a "collection of vignettes" and a "complete novel", as it were.[/color]

                        Granted a *lot* of work would be needed to mold it into a novel.
                        But darned, given your style of writing, I thought then (and
                        still do) that it would be just a grand novel. I have always
                        thought the same about Lawrence Kirby's style of writing too.
                        It isn't just the facts, it's the life that you give those facts
                        in your expression of the story you tell. And indeed, you *do*
                        write stories, not just a list of facts.

                        I also have an interest in the style in which traditional Native
                        stories are told. There is a striking similarity between how
                        you would describe the inner workings a compiler, which is an
                        object most of us cannot really "see" with our own eyes or touch
                        with our own fingers, and the way an Eskimo elder might explain
                        to children the complex structure of something like the umiaq
                        boats used for whaling here in Barrow, or perhaps something even
                        more abstract such as spiritual relationships between humans and
                        animals. (You can, for example, find a great number of these
                        stories written down, but it is very hard to find one that will
                        make you sit up at the end and say, "Wow, I *understand*!".

                        Usually the response is more like, "Okay, if they think so...")

                        When I was young I learned a great deal about parenting skills
                        from an "old" Yup'ik Eskimo man (I am now the age he was then,
                        so I no longer think he was so old), whose adult children used
                        to tell me they would sometimes get him talking in the evening
                        and they would try so hard to stay awake because it was so
                        interesting. But of course, eventually they'd fall asleep
                        anyway. One of his youngest daughters, who was about 19 or 20
                        at the time, told me every time she woke up after one of those
                        sessions, realizing that she had fallen asleep too soon and had
                        missed something, she'd swear next time she would stay awake
                        longer.


                        [interesting stuff snipped]
                        [color=blue]
                        >Computer programming is, at its heart, about abstraction --
                        >controlling complexity by removing irrelevant detail, at the point
                        >where it becomes irrelevant. I am not at all sure that tutorial-writing
                        >is about abstraction. It seems different, somehow. Certainly
                        >there are people who are quite good at one and terrible at the
                        >other. But some of the elements are the same: programs can be
                        >terribly complex, and often the only way to get them right is to
                        >simplify them. This gives rise to program structures, including
                        >data structures.[/color]

                        I wanted to leave the context above and below the following
                        sentence, and yet separate this one part out from the rest. To
                        me, the ability to accomplish this seems to be the key to most
                        writing for Usenet, but certainly for tutorials in particular.
                        [color=blue]
                        >Tutorials likewise need structure, so that details appear only
                        >where they are relevant.[/color]

                        Of course there's a lot of devils in the details of how to
                        accomplish that.
                        [color=blue]
                        >But the structures themselves are different, or at least, have
                        >quite different constraints.
                        >
                        >Well, that seems quite a few words for saying "I don't know", but
                        >I am prone to a certain prolixity. :-) There is really only one
                        >part that I am sure of: if you want to write, you must read, read,
                        >read.[/color]

                        Thanks Chris, I enjoyed your comments and perspective.

                        --
                        Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
                        Ukpeagvik (Barrow, Alaska) floyd@barrow.co m

                        Comment

                        Working...