program bug

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

    program bug

    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?

    Bill


  • Bill Cunningham

    #2
    Re: program bug

    Well I now see I left out fclose I will correct that but I don't think
    that's my problem.

    Bill


    Comment

    • Robert Gamble

      #3
      Re: program bug

      On May 2, 5: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;
      Not a portable return value from main.
      }
      if (argv[1]=="b") {
      This isn't doing what you think it is. This is comparing the address
      of first program argument to the address of the string literal "b"
      which will always be different. Either use (strcmp(argv[1], "b") ==
      0) or say:

      if (argv[1][0] == 'b') {

      which will compare the first letter of the first argument to the
      character constant 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 is not initialized, referencing its value invokes undefined
      behavior.
      a=fgetc(ifp);
      fputc(a,ofp);
      I think you want braces around these statements, yes?

      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

      • Robert Gamble

        #4
        Re: program bug

        On May 2, 5:53 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
        Well I now see I left out fclose I will correct that but I don't think
        that's my problem.
        >
        Bill
        There were a number of problems with the program but as long as the
        program was terminating normally the fclose() wasn't one of them, all
        files are properly closed during normal program termination (although
        it is usually a good idea to explicitly close files yourself).

        --
        Robert Gamble

        Comment

        • Bill Cunningham

          #5
          Re: program bug


          "Robert Gamble" <rgamble99@gmai l.comwrote in message
          news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@8g 2000hse.googleg roups.com...
          >a is not initialized, referencing its value invokes undefined
          behavior.
          >
          > a=fgetc(ifp);
          > fputc(a,ofp);
          In the beginning a was declared. Is it not enough in this case to simply
          declare an int? Or should I have done this int a=0; at the beginning of the
          program?

          Bill


          Comment

          • Bill Cunningham

            #6
            Re: program bug


            "Robert Gamble" <rgamble99@gmai l.comwrote in message
            news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@8g 2000hse.googleg roups.com...
            Not a portable return value from main.
            >
            > }
            > if (argv[1]=="b") {
            >
            This isn't doing what you think it is. This is comparing the address
            of first program argument to the address of the string literal "b"
            Was declaring b as a pointer to char proper? Or should a simple b of
            type char all that's needed?

            which will always be different. Either use (strcmp(argv[1], "b") ==
            0) or say:
            >
            if (argv[1][0] == 'b') {
            >
            which will compare the first letter of the first argument to the
            character constant b.
            I do not understand the above but it looks like a good way to write
            code. Why is a zero used as the second dimension of this array?

            Bill



            Comment

            • Joe Wright

              #7
              Re: program bug

              Bill Cunningham wrote:
              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?
              >
              Bill
              >
              >
              You can't use == to compare strings. That's what strcmp() is for. This
              is the first error I find, not the only one.

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

              Comment

              • Robert Gamble

                #8
                Re: program bug

                On May 2, 6:16 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
                "Robert Gamble" <rgambl...@gmai l.comwrote in message
                >
                news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@8g 2000hse.googleg roups.com...
                >
                a is not initialized, referencing its value invokes undefined
                behavior.
                >
                a=fgetc(ifp);
                fputc(a,ofp);
                >
                In the beginning a was declared. Is it not enough in this case to simply
                declare an int? Or should I have done this int a=0; at the beginning of the
                program?
                You need to store a value into a variable before you use the value of
                that variable, either through initialization (implicit or explicit) or
                assignment. If you don't do this then the variable may contain
                garbage ("indetermin ate value" in standards parlance), that garbage
                may be a trap value which will invoke UB when read. Note that the
                alternative I provided does not have this problem because "a" is
                assigned before its value is used.

                --
                Robert Gamble

                Comment

                • Ian Collins

                  #9
                  Re: program bug

                  Bill Cunningham wrote:
                  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?
                  >
                  Someone stole the whitespace?

                  Why are you comparing characters with string literals? Surly your
                  compiler gave you some warnings?

                  --
                  Ian Collins.

                  Comment

                  • Bill Cunningham

                    #10
                    Re: program bug


                    "Ian Collins" <ian-news@hotmail.co mwrote in message
                    news:681is1F2qc 9btU19@mid.indi vidual.net...
                    Why are you comparing characters with string literals? Surly your
                    compiler gave you some warnings?
                    >
                    Not as whisper. As it is typed as is.

                    Bill


                    Comment

                    • Robert Gamble

                      #11
                      Re: program bug

                      On May 2, 6:28 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                      Bill Cunningham wrote:
                      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?
                      >
                      Someone stole the whitespace?
                      >
                      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?

                      --
                      Robert Gamble

                      Comment

                      • Bill Cunningham

                        #12
                        Re: program bug


                        "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. These two
                        and maybe a third.

                        function { /*notice the one space after the first brace*/
                        } closing brace on a line by itself.
                        This is the style I use and another

                        function
                        {

                        body

                        }

                        With this style all braces are on lines by themselves.


                        Bill


                        Comment

                        • CBFalconer

                          #13
                          Re: program bug

                          Ian Collins wrote:
                          >
                          .... snip ...
                          >
                          Someone stole the whitespace?
                          >
                          Why are you comparing characters with string literals? Surly
                          your compiler gave you some warnings?
                          Surly compilers have that fault. :-)

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

                          • CBFalconer

                            #14
                            Re: program bug

                            Bill Cunningham wrote:
                            "Robert Gamble" <rgamble99@gmai l.comwrote in message
                            >
                            >a is not initialized, referencing its value invokes undefined
                            >behavior.
                            >>
                            > a=fgetc(ifp);
                            > fputc(a,ofp);
                            >
                            In the beginning a was declared. Is it not enough in this case to
                            simply declare an int? Or should I have done this int a=0; at the
                            beginning of the program?
                            You are using it before loading it. You want:

                            while (EOF != (a = fgetc(ifp))) fputc(a, ofp);

                            Note the use of blanks. As I recall there are other errors also.

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

                            • Ian Collins

                              #15
                              Re: program bug

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

                              --
                              Ian Collins.

                              Comment

                              Working...