program bug

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

    #16
    Re: program bug

    Ian Collins wrote:
    Robert Gamble wrote:
    >On May 2, 6:28 pm, Ian Collins <ian-n...@hotmail.co mwrote:
    >>Why are you comparing characters with string literals? Surly your
    >>compiler gave you some warnings?
    >My compiler didn't provide any warnings for this code, did yours?
    >>
    Oops, my bad, I spotted the comparisons an didn't spot they were with
    argv[n].
    >
    So what?

    if (argv[1] == "b")

    is not an error the compiler need diagnose. Both arguments are of type
    (char *) and the chances the two pointer values are equal is nil but the
    expression is completely valid.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---

    Comment

    • Ian Collins

      #17
      Re: program bug

      Joe Wright wrote:
      Ian Collins wrote:
      >Robert Gamble wrote:
      >>On May 2, 6:28 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      >>>Why are you comparing characters with string literals? Surly your
      >>>compiler gave you some warnings?
      >>My compiler didn't provide any warnings for this code, did yours?
      >>>
      >Oops, my bad, I spotted the comparisons an didn't spot they were with
      >argv[n].
      >>
      So what?
      >
      if (argv[1] == "b")
      >
      is not an error the compiler need diagnose. Both arguments are of type
      (char *) and the chances the two pointer values are equal is nil but the
      expression is completely valid.
      >
      I know, that's why I said I didn't spot the comparison was with argv[n].
      If I had, I wouldn't have mentioned compiler warnings.

      --
      Ian Collins.

      Comment

      • user923005

        #18
        Re: program bug

        On May 2, 2:40 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
            I have looked this program up and down and I don't see what's wrong with
        it. But it always breaks and gives me an error "mode error" no matter which
        mode binary or text I choose. This simple program is supposed to take as
        argv[1] a "b" or "t" for binary or text. It's not taking anything.
        >
        #include <stdio.h>
        >
        int main(int argc, char **argv) {
         char *b;
         int a;
         FILE *ifp,*ofp;
         if (argc!=4) {
         fprintf(stderr, "usage error\n");
         return -1;
         }
         if (argv[1]=="b") {
         b="rb";
         }
         if (argv[1]=="t") {
         b="rt";
         }
         if (argv[1]!="t"||argv[1]!="b") {
         fprintf(stderr, "mode error\n");
         return -1;
         }
         if ((ifp=fopen(arg v[2],b))==0) {
         fprintf(stderr, "open error i\n");
         return -1;
         }
         if ((ofp=fopen(arg v[3],b))==0) {
         fprintf(stderr, "open error o\n");
         return -1;
         }
         while(a!=EOF)
         a=fgetc(ifp);
         fputc(a,ofp);
         printf("done\n" );
         return 0;}
        >
            Is anyone good enough to glance at this and see what's wrong?
        Too many things for a glance. It requires a perusal.

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

        int main(int argc, char **argv)
        {
        char *imode = 0;
        char *omode = 0;
        char option;
        int ch = 0;

        FILE *ifp,
        *ofp;
        if (argc != 4) {
        fprintf(stderr, "usage error\n");
        return EXIT_FAILURE;
        }
        option = *argv[1];
        if (option == 'b') {
        imode = "rb";
        omode = "wb";
        }
        if (option == 't') {
        /* This may or may not do something useful on your system */
        /* The 't' in the mode is not portable, but is a fairly */
        /* popular extension for text mode. */
        imode = "rt";
        omode = "wt";
        }
        if (option != 't' && option != 'b') {
        fprintf(stderr, "mode error\n");
        return EXIT_FAILURE;
        }
        if ((ifp = fopen(argv[2], imode)) == 0) {
        fprintf(stderr, "open error i\n");
        return EXIT_FAILURE;
        }
        if ((ofp = fopen(argv[3], omode)) == 0) {
        fprintf(stderr, "open error o\n");
        return EXIT_FAILURE;
        }
        /* I guess that you want to write what you read, hence the {} */
        do {
        ch = fgetc(ifp);
        if (ch != EOF)
        fputc(ch, ofp);
        } while (ch != EOF);
        fclose(ifp);
        fclose(ofp);
        printf("done\n" );
        return 0;
        }

        Comment

        • Keith Thompson

          #19
          Re: program bug

          "Bill Cunningham" <nospam@nspam.c omwrites:
          "Ian Collins" <ian-news@hotmail.co mwrote in message
          news:681is1F2qc 9btU19@mid.indi vidual.net...
          >Someone stole the whitespace?
          >
          I was always told there were several choices in C indentation.
          [...]

          Yes, there are serveral choices. No indentation at all is not one of
          them.

          If you want help with your code, indent it. It doesn't matter much
          which indentation style you choose, but pick *something* that reflects
          the structure of your code. (To be clear, indentation refers to
          whitespace at the beginning of each line.)

          --
          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Richard Heathfield

            #20
            Re: program bug

            Keith Thompson said:
            "Bill Cunningham" <nospam@nspam.c omwrites:
            >"Ian Collins" <ian-news@hotmail.co mwrote in message
            >news:681is1F2q c9btU19@mid.ind ividual.net...
            >>Someone stole the whitespace?
            >>
            > I was always told there were several choices in C indentation.
            [...]
            >
            Yes, there are serveral choices. No indentation at all is not one of
            them.
            Wrong. No indentation at all *is* a valid choice. The OP is under no
            obligation whatsoever to write his code in a way that encourages people to
            help him.

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

            • Bill Cunningham

              #21
              Re: program bug


              "user923005 " <dcorbit@connx. comwrote in message
              news:6f66a742-0b5b-4e16-b314-dc5e818b6cf0@i7 6g2000hsf.googl egroups.com...
              #include <stdio.h>
              >
              int main(int argc, char **argv) {
              char *b;
              int a;
              FILE *ifp,*ofp;
              if (argc!=4) {
              fprintf(stderr, "usage error\n");
              return -1;
              }
              if (argv[1]=="b") {
              b="rb";
              }
              if (argv[1]=="t") {
              b="rt";
              }
              if (argv[1]!="t"||argv[1]!="b") {
              fprintf(stderr, "mode error\n");
              return -1;
              }
              if ((ifp=fopen(arg v[2],b))==0) {
              fprintf(stderr, "open error i\n");
              return -1;
              }
              if ((ofp=fopen(arg v[3],b))==0) {
              fprintf(stderr, "open error o\n");
              return -1;
              }
              while(a!=EOF)
              a=fgetc(ifp);
              fputc(a,ofp);
              printf("done\n" );
              return 0;}
              >
              Is anyone good enough to glance at this and see what's wrong?
              Too many things for a glance. It requires a perusal.

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

              int main(int argc, char **argv)
              {
              char *imode = 0;
              char *omode = 0;
              char option;
              int ch = 0;

              FILE *ifp,
              *ofp;
              if (argc != 4) {
              fprintf(stderr, "usage error\n");
              return EXIT_FAILURE;
              }
              option = *argv[1];
              if (option == 'b') {
              imode = "rb";
              omode = "wb";
              }
              if (option == 't') {
              /* This may or may not do something useful on your system */
              /* The 't' in the mode is not portable, but is a fairly */
              /* popular extension for text mode. */
              imode = "rt";
              omode = "wt";
              }
              if (option != 't' && option != 'b') {
              fprintf(stderr, "mode error\n");
              return EXIT_FAILURE;
              }
              if ((ifp = fopen(argv[2], imode)) == 0) {
              fprintf(stderr, "open error i\n");
              return EXIT_FAILURE;
              }
              if ((ofp = fopen(argv[3], omode)) == 0) {
              fprintf(stderr, "open error o\n");
              return EXIT_FAILURE;
              }
              /* I guess that you want to write what you read, hence the {} */
              do {
              ch = fgetc(ifp);
              if (ch != EOF)
              fputc(ch, ofp);
              } while (ch != EOF);
              fclose(ifp);
              fclose(ofp);
              printf("done\n" );
              return 0;
              }

              I will definately save this code and study it. I really do no use do so
              maybe this will open my eyes to it some.

              Bill


              Comment

              • vippstar@gmail.com

                #22
                Re: program bug

                On May 3, 6:52 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
                "user923005 " <dcor...@connx. comwrote in message
                >
                news:6f66a742-0b5b-4e16-b314-dc5e818b6cf0@i7 6g2000hsf.googl egroups.com...
                >
                >
                >
                #include <stdio.h>
                >
                int main(int argc, char **argv) {
                char *b;
                int a;
                FILE *ifp,*ofp;
                if (argc!=4) {
                fprintf(stderr, "usage error\n");
                return -1;
                }
                if (argv[1]=="b") {
                b="rb";
                }
                if (argv[1]=="t") {
                b="rt";
                }
                if (argv[1]!="t"||argv[1]!="b") {
                fprintf(stderr, "mode error\n");
                return -1;
                }
                if ((ifp=fopen(arg v[2],b))==0) {
                fprintf(stderr, "open error i\n");
                return -1;
                }
                if ((ofp=fopen(arg v[3],b))==0) {
                fprintf(stderr, "open error o\n");
                return -1;
                }
                while(a!=EOF)
                a=fgetc(ifp);
                fputc(a,ofp);
                printf("done\n" );
                return 0;}
                >
                Is anyone good enough to glance at this and see what's wrong?
                >
                Too many things for a glance. It requires a perusal.
                >
                #include <stdio.h>
                #include <stdlib.h>
                >
                int main(int argc, char **argv)
                {
                char *imode = 0;
                char *omode = 0;
                char option;
                int ch = 0;
                >
                FILE *ifp,
                *ofp;
                if (argc != 4) {
                fprintf(stderr, "usage error\n");
                return EXIT_FAILURE;
                }
                option = *argv[1];
                if (option == 'b') {
                imode = "rb";
                omode = "wb";
                }
                if (option == 't') {
                /* This may or may not do something useful on your system */
                /* The 't' in the mode is not portable, but is a fairly */
                /* popular extension for text mode. */
                imode = "rt";
                omode = "wt";
                Hahah, what else will the troll come up with? You're entertaining, but
                it's sad that posters take you seriously.

                Comment

                • Bill Cunningham

                  #23
                  Re: program bug


                  "Robert Gamble" <rgamble99@gmai l.comwrote in message
                  news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@8g 2000hse.googleg roups.com...

                  [snip]

                  I think you want braces around these statements, yes?
                  I don't know. I've ran code before and seen a lot of people use while
                  and statements after it and the code works. No braces. So I usually use it
                  like that. It's not a typo I don't usually use braces with while.
                  Try (not tested):
                  >
                  while ( (a = fgetc(ifp)) != EOF && fputc(a, ofp) != EOF )
                  ;
                  >
                  > printf("done\n" );
                  >
                  puts("done"); would work equally well.
                  >
                  > return 0;}
                  >>
                  > Is anyone good enough to glance at this and see what's wrong?
                  >>
                  >Bill
                  >
                  --
                  Robert Gamble

                  Comment

                  • Keith Thompson

                    #24
                    Re: program bug

                    "Bill Cunningham" <nospam@nspam.c omwrites:
                    "Robert Gamble" <rgamble99@gmai l.comwrote in message
                    news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@8g 2000hse.googleg roups.com...
                    >
                    [snip]
                    >
                    >
                    >I think you want braces around these statements, yes?
                    >
                    I don't know. I've ran code before and seen a lot of people use while
                    and statements after it and the code works. No braces. So I usually use it
                    like that. It's not a typo I don't usually use braces with while.
                    [...]

                    Do you understand what the braces mean?

                    Here's the tail end of the code Robert was commenting on:

                    [...]
                    while(a!=EOF)
                    a=fgetc(ifp);
                    fputc(a,ofp);
                    printf("done\n" );
                    return 0;}

                    Putting the closing brace immediately after the semicolon like that is
                    horribly bad style. Putting everything at the same indentation level
                    is also horribly bad style. The compiler doesn't care, but anyone
                    trying to read your code does.

                    The syntax of a while statement is:

                    while ( expression ) statement

                    Note that that's a *single* statement. If you want the body to be
                    multiple statements, you need to wrap them in braces to create a
                    compound statement, which is itself as single statement. (The same
                    thing applies to if statements; you correctly used braces for those,
                    so I don't know why the while loop was such a problem.)

                    The code I quoted is equivalent to this:

                    [...]
                    while (a != EOF)
                    a = fgetc(ifp);
                    fputc(a, ofp);
                    printf("done\n" );
                    return 0;
                    }

                    With proper indentation, you can see that the while controls only a
                    single statement, ``a = fgetc(ifp);''. The fputc call will occur
                    exactly once, after the while loop terminates. I can't be certain,
                    but I *think* you wanted both the fgetc and fputc calls to be in the
                    body of the loop. To do that, you need braces:

                    [...]
                    while (a != EOF) {
                    a = fgetc(ifp);
                    fputc(a, ofp);
                    }
                    printf("done\n" );
                    return 0;
                    }

                    In my opinion, it's a good habit always to use braces even if you only
                    want only a single statement in the body. It makes the code more
                    consistent and easier to maintain if you later want to add a second
                    statement. (Others will disagree.)

                    Judicious use of whitespace, especially around operators and after
                    commas, also makes the code much easier to read.

                    From now on, if you post a chunk of nearly illegible code like what
                    you posted upthread, with no indentation and virtually no whitespace,
                    I will ask you to format it properly before I'll even consider helping
                    you fix any other problems.

                    --
                    Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                    Nokia
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Bill Cunningham

                      #25
                      Re: program bug


                      "Keith Thompson" <kst-u@mib.orgwrote in message
                      news:87bq3nys73 .fsf@kvetch.smo v.org...
                      The syntax of a while statement is:
                      >
                      while ( expression ) statement
                      >
                      Note that that's a *single* statement. If you want the body to be
                      multiple statements, you need to wrap them in braces to create a
                      compound statement,
                      I see.

                      which is itself as single statement. (The same
                      thing applies to if statements; you correctly used braces for those,
                      I think braces are required with if.
                      so I don't know why the while loop was such a problem.)
                      >
                      The code I quoted is equivalent to this:
                      >
                      [...]
                      while (a != EOF)
                      a = fgetc(ifp);
                      fputc(a, ofp);
                      printf("done\n" );
                      return 0;
                      }
                      >
                      With proper indentation, you can see that the while controls only a
                      single statement, ``a = fgetc(ifp);''. The fputc call will occur
                      exactly once, after the while loop terminates. I can't be certain,
                      but I *think* you wanted both the fgetc and fputc calls to be in the
                      body of the loop. To do that, you need braces:
                      >
                      [...]
                      while (a != EOF) {
                      a = fgetc(ifp);
                      fputc(a, ofp);
                      }
                      printf("done\n" );
                      return 0;
                      }
                      >
                      In my opinion, it's a good habit always to use braces even if you only
                      want only a single statement in the body. It makes the code more
                      consistent and easier to maintain if you later want to add a second
                      statement. (Others will disagree.)
                      >
                      Judicious use of whitespace, especially around operators and after
                      commas, also makes the code much easier to read.
                      >
                      From now on, if you post a chunk of nearly illegible code like what
                      you posted upthread, with no indentation and virtually no whitespace,
                      I will ask you to format it properly before I'll even consider helping
                      you fix any other problems.
                      OK

                      Bill


                      Comment

                      • vippstar@gmail.com

                        #26
                        Re: program bug

                        On May 4, 1:58 am, "Bill Cunningham" <nos...@nspam.c omwrote:
                        "Keith Thompson" <ks...@mib.orgw rote in message
                        >
                        news:87bq3nys73 .fsf@kvetch.smo v.org...
                        >
                        The syntax of a while statement is:
                        >
                        while ( expression ) statement
                        >
                        Note that that's a *single* statement. If you want the body to be
                        multiple statements, you need to wrap them in braces to create a
                        compound statement,
                        >
                        I see.
                        >
                        which is itself as single statement. (The same
                        >
                        thing applies to if statements; you correctly used braces for those,
                        >
                        I think braces are required with if.
                        No they are not. You know it, you could try it, you could read about
                        it.
                        *OR* you could just read what mr Keith said; *the* *same* *thing*
                        *applies* *to* *if* *statements*.
                        Just once more, just in case:
                        *the* *same* *thing* *applies* *to* *if* *statements*.
                        Now, every day you wake up, you should run this commant in your
                        terminal: 'yes the same thing applies to if statements', until you
                        memorize it. Then you can chant it quietly, until you understand it.

                        Mr Keith (and anyone else who actually cared to reply to this troll)
                        I'm really tired of seeing clc replying to him.
                        Assuming he is actually not able to learn after so many years, and
                        assuming he can't understand what he reads in usenet messages (as
                        shown just now), you are simply not helping him. Just how stupid does
                        one have to be to say "I think B isn't" when you say "A is, same
                        applies for B?"
                        And what's up with all the horrible indentation he comes up with every
                        single time, the horrible questions "is anyone good enough to [...]"
                        in his original message, which could simply be answered with
                        "yes"/"no", the repeating mistakes, the tendency to reply without
                        quoting, taking quotes out of context (notice his reply to Mr Gamble)

                        Honestly, I'm tired of these threads. I also think I make a fool of
                        myself, since such replies most likely entertain him and the other
                        trolls...

                        Comment

                        • Keith Thompson

                          #27
                          Re: program bug

                          "Bill Cunningham" <nospam@nspam.c omwrites:
                          "Keith Thompson" <kst-u@mib.orgwrote in message
                          news:87bq3nys73 .fsf@kvetch.smo v.org...
                          >
                          >The syntax of a while statement is:
                          >>
                          > while ( expression ) statement
                          >>
                          >Note that that's a *single* statement. If you want the body to be
                          >multiple statements, you need to wrap them in braces to create a
                          >compound statement,
                          >
                          I see.
                          >
                          which is itself as single statement. (The same
                          >thing applies to if statements; you correctly used braces for those,
                          >
                          I think braces are required with if.
                          [...]

                          You're guessing. Don't guess. Look it up.

                          --
                          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                          Nokia
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • CBFalconer

                            #28
                            Re: program bug

                            vippstar@gmail. com wrote:
                            >
                            .... snip ...
                            >
                            Mr Keith (and anyone else who actually cared to reply to this
                            troll) I'm really tired of seeing clc replying to him. Assuming
                            he is actually not able to learn after so many years, and assuming
                            he can't understand what he reads in usenet messages (as shown
                            just now), you are simply not helping him. Just how stupid does
                            one have to be to say "I think B isn't" when you say "A is, same
                            applies for B?"
                            ....

                            Suit yourself. If you don't like it, it takes a few seconds to
                            plonk Cunningham, and possibly to threadplonk any threads he
                            starts. Then you won't be bothered. Some of us admire his
                            stick-to-it-ness. We don't really expect him to learn, but he has
                            at times. The problem is that he then forgets it all.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            ** Posted from http://www.teranews.com **

                            Comment

                            • Default User

                              #29
                              Re: program bug

                              Keith Thompson wrote:

                              From now on, if you post a chunk of nearly illegible code like what
                              you posted upthread, with no indentation and virtually no whitespace,
                              I will ask you to format it properly before I'll even consider helping
                              you fix any other problems.
                              It will feel good once you stop beating your head against the wall.





                              Brian

                              Comment

                              • Keith Thompson

                                #30
                                Re: program bug

                                "Default User" <defaultuserbr@ yahoo.comwrites :
                                Keith Thompson wrote:
                                >
                                >From now on, if you post a chunk of nearly illegible code like what
                                >you posted upthread, with no indentation and virtually no whitespace,
                                >I will ask you to format it properly before I'll even consider helping
                                >you fix any other problems.
                                >
                                It will feel good once you stop beating your head against the wall.
                                Thanks for the advice.

                                Again.

                                --
                                Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                                Nokia
                                "We must do something. This is something. Therefore, we must do this."
                                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                                Comment

                                Working...