code question

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

    code question

    I have this code I would like to clean up.

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    double x, y, a, b;
    FILE *fp;
    x = strtod(argv[1], NULL);
    y = strtod(argv[2], NULL);
    a = strtod(argv[3], NULL);
    b = strtod(argv[4], NULL);
    if ((fp = fopen(argv[4], "a")) == NULL) {
    puts("fopen error");
    exit(-1);
    }
    fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
    if (fclose(fp) == EOF) {
    puts("fclose error");
    exit(-1);
    }
    return 0;
    }

    If the program is run with no arguments I get a seg fault. If it is run with
    4, no problem. If it is run with less than four (that includes argv[0]) then
    the program doesn't want to run right. How would I be able to use this
    program with say one or two argvs ?

    Bill


  • Richard Heathfield

    #2
    Re: code question

    Bill Cunningham said:
    I have this code I would like to clean up.
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    int main(int argc, char *argv[])
    {
    double x, y, a, b;
    FILE *fp;
    x = strtod(argv[1], NULL);
    y = strtod(argv[2], NULL);
    a = strtod(argv[3], NULL);
    b = strtod(argv[4], NULL);
    if ((fp = fopen(argv[4], "a")) == NULL) {
    puts("fopen error");
    exit(-1);
    }
    fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
    if (fclose(fp) == EOF) {
    puts("fclose error");
    exit(-1);
    }
    return 0;
    }
    >
    If the program is run with no arguments I get a seg fault. If it is run
    with 4, no problem. If it is run with less than four (that includes
    argv[0]) then the program doesn't want to run right. How would I be able
    to use this program with say one or two argvs ?
    #include <stdio.h>
    #include <stdlib.h>

    #define DEFAULT_FILE_NA ME "foo.bar"

    int main(int argc, char *argv[])
    {
    double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
    const char *filename = DEFAULT_FILE_NA ME;

    FILE *fp = NULL;
    if(argc 1)
    {
    x = strtod(argv[1], NULL);
    }
    if(argc 2)
    {
    y = strtod(argv[2], NULL);
    }
    if(argc 3)
    {
    a = strtod(argv[3], NULL);
    }
    if(argc 4)
    {
    b = strtod(argv[4], NULL);
    filename = argv[4];
    }
    if ((fp = fopen(filename, "a")) == NULL) {
    puts("fopen error");
    exit(EXIT_FAILU RE);
    }
    fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
    if (fclose(fp) == EOF) {
    puts("fclose error");
    exit(EXIT_FAILU RE);
    }
    return 0;
    }

    --
    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

    Comment

    • Ian Collins

      #3
      Re: code question

      Bill Cunningham wrote:
      I have this code I would like to clean up.
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      int main(int argc, char *argv[])
      {
      double x, y, a, b;
      FILE *fp;
      x = strtod(argv[1], NULL);
      y = strtod(argv[2], NULL);
      a = strtod(argv[3], NULL);
      b = strtod(argv[4], NULL);
      if ((fp = fopen(argv[4], "a")) == NULL) {
      puts("fopen error");
      exit(-1);
      }
      fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
      if (fclose(fp) == EOF) {
      puts("fclose error");
      exit(-1);
      }
      return 0;
      }
      >
      If the program is run with no arguments I get a seg fault. If it is run with
      4, no problem. If it is run with less than four (that includes argv[0]) then
      the program doesn't want to run right. How would I be able to use this
      program with say one or two argvs ?
      >
      Your requirements are incomplete.

      What should happen if there are less than four arguments? Should a file
      be opened? Should an error message be output?

      Without these details, there a number of conflicting possible solutions.

      --
      Ian Collins.

      Comment

      • Bill Cunningham

        #4
        Re: code question


        "Richard Heathfield" <rjh@see.sig.in validwrote in message
        news:wL2dnfcbMf wWj0nVRVnyigA@b t.com...
        #include <stdio.h>
        #include <stdlib.h>
        >
        #define DEFAULT_FILE_NA ME "foo.bar"
        >
        int main(int argc, char *argv[])
        {
        double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
        const char *filename = DEFAULT_FILE_NA ME;
        >
        FILE *fp = NULL;
        if(argc 1)
        {
        x = strtod(argv[1], NULL);
        }
        if(argc 2)
        {
        y = strtod(argv[2], NULL);
        }
        if(argc 3)
        {
        a = strtod(argv[3], NULL);
        }
        if(argc 4)
        {
        b = strtod(argv[4], NULL);
        filename = argv[4];
        }
        if ((fp = fopen(filename, "a")) == NULL) {
        puts("fopen error");
        exit(EXIT_FAILU RE);
        }
        fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
        if (fclose(fp) == EOF) {
        puts("fclose error");
        exit(EXIT_FAILU RE);
        }
        return 0;
        }
        Thanks Richard I know I can always count on you. I wondered about argc
        and its uses.

        Bill


        Comment

        • Martin Ambuhl

          #5
          Re: code question

          Bill Cunningham wrote:
          I have this code I would like to clean up.
          Well, it certainly needs cleaning up, so you have got this part right.
          See below.
          If the program is run with no arguments I get a seg fault. If it is run with
          4, no problem. If it is run with less than four (that includes argv[0]) then
          the program doesn't want to run right. How would I be able to use this
          program with say one or two argvs ?
          There are many ways to hand differing numbers of arguments to a program.
          Here is one way to start on yours, except that making your filename
          argv[4] makes the exercise pointless.

          #include <stdio.h>
          #include <stdlib.h>

          int main(int argc, char *argv[])
          {
          double x = 0, y = 0, a = 0, b = 0;
          FILE *fp;
          switch (argc) {
          case 5:
          b = strtod(argv[4], NULL);
          case 4:
          a = strtod(argv[3], NULL);
          case 3:
          y = strtod(argv[2], NULL);
          case 2:
          x = strtod(argv[1], NULL);
          case 1:
          break;
          default:
          fputs("I don't know how to handle that many arguments.\n",
          stderr);
          exit(EXIT_FAILU RE);
          }
          if (argc < 5) {
          fprintf(stderr, "%s",
          "I just wasted some time assigning values to x, y, a, &"
          "b\nsince the fourth argument (argv[4]) is needed for a"
          " filename\nand you didn't supply it.\n");
          exit(EXIT_FAILU RE);
          }
          if ((fp = fopen(argv[4] /* this is nonsense, of course, if
          argv < 5 */ , "a")) == NULL) {
          fputs("fopen error\n", stderr);
          exit(EXIT_FAILU RE); /* fixed non-portable 'exit(-1)' */
          }
          fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
          if (fclose(fp) == EOF) {
          fputs("fclose error\n", stderr);
          exit(EXIT_FAILU RE); /* fixed non-portable 'exit(-1)' */
          }
          return 0;
          }

          Comment

          • Bill Cunningham

            #6
            Re: code question


            "Martin Ambuhl" <mambuhl@earthl ink.netwrote in message
            news:gb1544$bmn $1@registered.m otzarella.org.. .
            >
            Well, it certainly needs cleaning up, so you have got this part right. See
            below.
            >
            >
            There are many ways to hand differing numbers of arguments to a program.
            Here is one way to start on yours, except that making your filename
            argv[4] makes the exercise pointless.
            >
            #include <stdio.h>
            #include <stdlib.h>
            >
            int main(int argc, char *argv[])
            {
            double x = 0, y = 0, a = 0, b = 0;
            FILE *fp;
            Great. Switch. I've never used it before.
            switch (argc) {
            case 5:
            b = strtod(argv[4], NULL);
            case 4:
            a = strtod(argv[3], NULL);
            case 3:
            y = strtod(argv[2], NULL);
            case 2:
            x = strtod(argv[1], NULL);
            case 1:
            break;
            default:
            fputs("I don't know how to handle that many arguments.\n",
            stderr);
            What's going on here? Is the default for more than the number of argv's
            that the program calls for ? Why is there a break and not just default?
            exit(EXIT_FAILU RE);
            }
            if (argc < 5) {
            fprintf(stderr, "%s",
            "I just wasted some time assigning values to x, y, a, &"
            "b\nsince the fourth argument (argv[4]) is needed for a"
            " filename\nand you didn't supply it.\n");
            exit(EXIT_FAILU RE);
            }
            if ((fp = fopen(argv[4] /* this is nonsense, of course, if
            argv < 5 */ , "a")) == NULL) {
            fputs("fopen error\n", stderr);
            exit(EXIT_FAILU RE); /* fixed non-portable 'exit(-1)' */
            }
            fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
            if (fclose(fp) == EOF) {
            fputs("fclose error\n", stderr);
            exit(EXIT_FAILU RE); /* fixed non-portable 'exit(-1)' */
            }
            return 0;
            }
            If exit(-1) is non -portable. Which I was afraid of what about exit(1)?
            Or does the standard only allow exit(EXIT_FAILU RE);

            Bill


            Comment

            • Bill Cunningham

              #7
              Re: code question


              "Ian Collins" <ian-news@hotmail.co mwrote in message
              news:6jij2sF389 73U4@mid.indivi dual.net...
              Your requirements are incomplete.
              >
              What should happen if there are less than four arguments? Should a file
              be opened? Should an error message be output?
              >
              Without these details, there a number of conflicting possible solutions.
              >
              Yes if there are less than four arguments a file should be opened. There
              must be a way to only write the fprintf stuff once though.

              Bill


              Comment

              • Ian Collins

                #8
                Re: code question

                Bill Cunningham wrote:
                "Ian Collins" <ian-news@hotmail.co mwrote in message
                news:6jij2sF389 73U4@mid.indivi dual.net...
                >
                >Your requirements are incomplete.
                >>
                >What should happen if there are less than four arguments? Should a file
                >be opened? Should an error message be output?
                >>
                >Without these details, there a number of conflicting possible solutions.
                >>
                Yes if there are less than four arguments a file should be opened. There
                must be a way to only write the fprintf stuff once though.
                >
                The how about

                #include <stdio.h>
                #include <stdlib.h>

                int main( int argc, char **argv )
                {
                double data[4] = {0.0};

                if ( argc 5 ) {
                puts("Too many arguments");
                exit(EXIT_FAILU RE);
                }

                unsigned n = 1;

                while ( n < argc ) {
                data[n-1] = strtod(argv[n], NULL);
                ++n;
                }

                FILE *fp = fopen(argv[n-1], "a");

                if ( fp == NULL ) {
                puts("fopen error");
                exit(EXIT_FAILU RE);
                }

                fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", data[0], data[1], data[2],
                data[3]);

                if (fclose(fp) == EOF ) {
                puts("fclose error");
                exit(EXIT_FAILU RE);
                }

                return 0;
                }

                --
                Ian Collins.

                Comment

                • Andrew Poelstra

                  #9
                  Re: code question

                  On 2008-09-19, Bill Cunningham <nospam@nspam.i nvalidwrote:
                  >
                  Thanks Richard I know I can always count on you. I wondered about argc
                  and its uses.
                  >
                  >
                  In addition to argc, argv is also NULL terminated (IIRC!), so
                  you can loop through it without needed argc to find out when
                  to stop.

                  while(*argv)
                  {
                  process(*argv);
                  ++argv;
                  }

                  --
                  Andrew Poelstra apoelstra@wpsof tware.net
                  That was a joke. Jokes in mathematics, are sometimes not funny.
                  -Veselin Jungic

                  Comment

                  • Richard Heathfield

                    #10
                    Re: code question

                    Andrew Poelstra said:

                    <snip>
                    In addition to argc, argv is also NULL terminated (IIRC!),
                    YRC. argv[argc] is guaranteed to be a null pointer (even if argc is 0).

                    <snip>

                    --
                    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

                    Comment

                    • Richard

                      #11
                      Re: code question

                      "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                      I have this code I would like to clean up.
                      >
                      #include <stdio.h>
                      #include <stdlib.h>
                      >
                      int main(int argc, char *argv[])
                      {
                      double x, y, a, b;
                      FILE *fp;
                      x = strtod(argv[1], NULL);
                      y = strtod(argv[2], NULL);
                      a = strtod(argv[3], NULL);
                      b = strtod(argv[4], NULL);
                      if ((fp = fopen(argv[4], "a")) == NULL) {
                      puts("fopen error");
                      exit(-1);
                      }
                      fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
                      if (fclose(fp) == EOF) {
                      puts("fclose error");
                      exit(-1);
                      }
                      return 0;
                      }
                      >
                      If the program is run with no arguments I get a seg fault. If it is
                      run with
                      Did you try it with a debugger? No? Then give up. Not that you need one
                      to spot the error. 10/10 for persistence. O/10 for originality. These
                      posts are getting a tad tiresome.
                      4, no problem. If it is run with less than four (that includes argv[0]) then
                      the program doesn't want to run right. How would I be able to use this
                      program with say one or two argvs ?
                      >
                      Bill
                      >
                      >
                      --

                      Comment

                      • Richard

                        #12
                        Re: code question

                        "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                        "Richard Heathfield" <rjh@see.sig.in validwrote in message
                        news:wL2dnfcbMf wWj0nVRVnyigA@b t.com...
                        >
                        >#include <stdio.h>
                        >#include <stdlib.h>
                        >>
                        >#define DEFAULT_FILE_NA ME "foo.bar"
                        >>
                        >int main(int argc, char *argv[])
                        >{
                        > double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
                        > const char *filename = DEFAULT_FILE_NA ME;
                        >>
                        > FILE *fp = NULL;
                        > if(argc 1)
                        > {
                        > x = strtod(argv[1], NULL);
                        > }
                        > if(argc 2)
                        > {
                        > y = strtod(argv[2], NULL);
                        > }
                        > if(argc 3)
                        > {
                        > a = strtod(argv[3], NULL);
                        > }
                        > if(argc 4)
                        > {
                        > b = strtod(argv[4], NULL);
                        > filename = argv[4];
                        > }
                        > if ((fp = fopen(filename, "a")) == NULL) {
                        > puts("fopen error");
                        > exit(EXIT_FAILU RE);
                        > }
                        > fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
                        > if (fclose(fp) == EOF) {
                        > puts("fclose error");
                        > exit(EXIT_FAILU RE);
                        > }
                        > return 0;
                        >}
                        >
                        Thanks Richard I know I can always count on you. I wondered about argc
                        and its uses.
                        >
                        Bill
                        You wondered about argc?

                        You never thought to

                        (a) see where it crashed using a debugger?
                        (b) examine the values of argc and argv in a debugger?

                        or

                        (c) read up about them in your copy of K&R2? Heathfield must really need
                        some soul cleansing if he responded to you with a total clean up with
                        no admonitions.

                        Comment

                        • Bill Cunningham

                          #13
                          Re: code question


                          "Richard" <rgrdev@gmail.c omwrote in message
                          news:gb1clg$bg$ 1@registered.mo tzarella.org...
                          Did you try it with a debugger? No? Then give up. Not that you need one
                          to spot the error. 10/10 for persistence. O/10 for originality. These
                          posts are getting a tad tiresome.
                          So are yours Richard Good God so are yours.

                          Have a nice life..



                          Comment

                          • Bill Cunningham

                            #14
                            Re: code question

                            Someone pointed out to me Richard that an answer to a question I raised
                            about bitwise operators was on page 49 or kandr2. I read, and re-read, and
                            re-read page 49 about 3 times and strained to keep my focus on reading and
                            the text. The question was why is the unary operator ~ shown like this:

                            x~34;
                            and not x=x~34;

                            Is the question indeed answered on that page?

                            Bill


                            Comment

                            • Barry Schwarz

                              #15
                              Re: code question

                              On Fri, 19 Sep 2008 16:52:17 -0400, "Bill Cunningham"
                              <nospam@nspam.i nvalidwrote:
                              I have this code I would like to clean up.
                              >
                              >#include <stdio.h>
                              >#include <stdlib.h>
                              >
                              >int main(int argc, char *argv[])
                              >{
                              double x, y, a, b;
                              FILE *fp;
                              x = strtod(argv[1], NULL);
                              y = strtod(argv[2], NULL);
                              a = strtod(argv[3], NULL);
                              b = strtod(argv[4], NULL);
                              So the fourth command line argument should be a number.
                              if ((fp = fopen(argv[4], "a")) == NULL) {
                              At the same time, it should be a file name.

                              That doesn't strike you as slightly odd?

                              --
                              Remove del for email

                              Comment

                              Working...