K&R2 , exercise 7.6

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

    K&R2 , exercise 7.6

    PURPOSE :: see statement in comments

    GOT: Segmentation Fault

    I guess the segfault is sourced in the compile-time warning but I am
    giving a char* to the function already.




    /* K&R2, section 7.7, exercise 7.6
    *
    * write a program to compare 2 files, printing that first line
    * where they differ.
    *
    */


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

    enum MAXSIZE { ARRSIZE=1000 };

    void compare_files( FILE*, FILE* );
    void print_line( char* );


    int main( int argc, char* argv[] )
    {
    FILE *pf1, *pf2;

    if( argc !=3 )
    {
    fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
    exit(EXIT_FAILU RE);
    }


    /* open files */
    pf1 = fopen( *++argv, "r" );
    pf2 = fopen( *++argv, "r" );

    /* error check */
    if( pf1 == NULL || pf2 == NULL )
    {
    fprintf( stderr, "error opening files\n");
    exit(EXIT_FAILU RE);
    }
    else
    {
    compare_files( pf1,pf2 );
    }



    /* don't forget to close the files */
    fclose( pf1 );
    fclose( pf2 );


    return 0;
    }



    /* compare 2 files */
    void compare_files( FILE* pf1, FILE* pf2 )
    {
    int c1, c2, match;
    char *line1, *line2;
    char *begin_line1, *begin_line2;

    match = 1;

    while( ((line1 = fgets( line1, ARRSIZE, pf1 )) != NULL) ||
    ((line2 = fgets( line2, ARRSIZE, pf2 )) != NULL))
    {
    begin_line1 = line1;
    begin_line2 = line2;

    for( c1 = *line1, c2 = *line2; c1 != '\0' && c2 != '\0'; ++line1,
    ++line2 )
    {
    if ( c1 != c2 )
    {
    match = 0;
    print_line( begin_line1 );
    printf("\n-----------------------\n"); print_line( begin_line2 );
    }
    }
    }
    }



    void print_line( char* line )
    {
    printf("%s\n", *line++);
    }

    =============== === OUTPUT =============== =======
    [arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
    7-6.c: In function `print_line':
    7-6.c:88: warning: format argument is not a pointer (arg 2)

    [arnuld@raj C]$ ./a.out
    You iDiOT, I expect 2 files as input.

    [arnuld@raj C]$ ./a.out 7-6.c 5-4.c
    Segmentation fault


    --

    my email ID is at the above address

  • Richard Heathfield

    #2
    Re: K&amp;R2 , exercise 7.6

    arnuld said:
    PURPOSE :: see statement in comments
    >
    GOT: Segmentation Fault
    >
    <snip>
    int c1, c2, match;
    char *line1, *line2;
    Where do these point? (Hint: nowhere in particular.)
    char *begin_line1, *begin_line2;
    >
    match = 1;
    >
    while( ((line1 = fgets( line1, ARRSIZE, pf1 )) != NULL) ||
    ((line2 = fgets( line2, ARRSIZE, pf2 )) != NULL))
    They certainly don't point to ARRSIZE bytes of memory that you have the
    right to update.

    <snip>
    void print_line( char* line )
    {
    printf("%s\n", *line++);
    void print_line(cons t char *line)
    {
    puts(line);
    }

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

    • arnuld

      #3
      Re: K&amp;R2 , exercise 7.6

      On Mon, 21 Apr 2008 10:02:16 +0000, Richard Heathfield wrote:
      arnuld said:
      Where do these point? (Hint: nowhere in particular.)

      OK, here is the 2nd version:


      /* K&R2, section 7.7, exercise 7.6
      *
      * write a program to compare 2 files, printing that first line
      * where they differ.
      *
      * version 1.1
      */


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

      enum MAXSIZE { ARRSIZE=1000 };

      void compare_files( FILE*, FILE* );
      void print_line( const char* );


      int main( int argc, char* argv[] )
      {
      FILE *pf1, *pf2;

      if( argc !=3 )
      {
      fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
      exit(EXIT_FAILU RE);
      }


      /* open files */
      pf1 = fopen( *++argv, "r" );
      pf2 = fopen( *++argv, "r" );

      /* error check */
      if( pf1 == NULL || pf2 == NULL )
      {
      fprintf( stderr, "error opening files\n");
      exit(EXIT_FAILU RE);
      }
      else
      {
      compare_files( pf1,pf2 );
      }



      /* don't forget to close the files */
      fclose( pf1 );
      fclose( pf2 );


      return 0;
      }



      /* compare 2 files */
      void compare_files( FILE* pf1, FILE* pf2 )
      {
      int match;
      char c1, c2;
      char line1[ARRSIZE];
      char line2[ARRSIZE];
      char *begin_line1, *begin_line2, *p1, *p2;


      match = 1;

      while( (fgets( line1, ARRSIZE, pf1 ) != NULL) ||
      (fgets( line2, ARRSIZE, pf2 ) != NULL))
      {
      begin_line1 = line1;
      begin_line2 = line2;

      p1 = line1;
      p2 = line2;
      for( c1 = *p1, c2 = *p2; c1 != '\0' && c2 != '\0'; ++p1, ++p2 )
      {
      if ( c1 != c2 )
      {
      match = 0;
      print_line( begin_line1 );
      printf("\n-----------------------\n"); print_line( begin_line2 );
      }
      }
      }
      }



      void print_line( const char* line )
      {
      while( *line != '\0')
      {
      puts(*line++);
      }
      }

      =============== = OUTPUT =============== ========

      [arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
      7-6.c: In function `print_line':
      7-6.c:96: warning: passing arg 1 of `puts' makes pointer from integer
      without a cast
      [arnuld@raj C]$ ./a.out 7-6.c replace-blanks.c Segmentationfau lt
      [arnuld@raj C]$







      --

      my email ID is at the above address

      Comment

      • Chris Dollin

        #4
        Re: K&amp;R2 , exercise 7.6

        arnuld wrote:
        void print_line( const char* line )
        {
        while( *line != '\0')
        {
        puts(*line++);
        }
        }
        >
        =============== = OUTPUT =============== ========
        >
        [arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
        7-6.c: In function `print_line':
        7-6.c:96: warning: passing arg 1 of `puts' makes pointer from integer
        without a cast
        [arnuld@raj C]$ ./a.out 7-6.c replace-blanks.c Segmentationfau lt
        [arnuld@raj C]$
        Ask yourself: what does `puts` accept as an parameter? What is
        the type of the expression `*line++`? Are C characters a kind
        of integer? Do you really expect an integer to be freely and
        automatically converted to an integer?

        --
        "It took a very long time, much longer than the most /Sector General/
        generous estimates."

        Hewlett-Packard Limited Cain Road, Bracknell, registered no:
        registered office: Berks RG12 1HN 690597 England

        Comment

        • Joachim Schmitz

          #5
          Re: K&amp;R2 , exercise 7.6

          arnuld wrote:
          >On Mon, 21 Apr 2008 10:02:16 +0000, Richard Heathfield wrote:
          >
          >arnuld said:
          >
          >Where do these point? (Hint: nowhere in particular.)
          >
          >
          OK, here is the 2nd version:
          >
          >
          /* K&R2, section 7.7, exercise 7.6
          *
          * write a program to compare 2 files, printing that first line
          * where they differ.
          *
          * version 1.1
          */
          >
          >
          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>
          >
          enum MAXSIZE { ARRSIZE=1000 };
          >
          void compare_files( FILE*, FILE* );
          void print_line( const char* );
          >
          >
          int main( int argc, char* argv[] )
          {
          FILE *pf1, *pf2;
          >
          if( argc !=3 )
          {
          fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
          exit(EXIT_FAILU RE);
          }
          >
          >
          /* open files */
          pf1 = fopen( *++argv, "r" );
          pf2 = fopen( *++argv, "r" );
          >
          /* error check */
          if( pf1 == NULL || pf2 == NULL )
          {
          fprintf( stderr, "error opening files\n");
          exit(EXIT_FAILU RE);
          }
          else
          {
          compare_files( pf1,pf2 );
          }
          >
          >
          >
          /* don't forget to close the files */
          fclose( pf1 );
          fclose( pf2 );
          >
          >
          return 0;
          }
          >
          >
          >
          /* compare 2 files */
          void compare_files( FILE* pf1, FILE* pf2 )
          {
          int match;
          char c1, c2;
          char line1[ARRSIZE];
          char line2[ARRSIZE];
          char *begin_line1, *begin_line2, *p1, *p2;
          >
          >
          match = 1;
          >
          while( (fgets( line1, ARRSIZE, pf1 ) != NULL) ||
          (fgets( line2, ARRSIZE, pf2 ) != NULL))
          {
          begin_line1 = line1;
          begin_line2 = line2;
          >
          p1 = line1;
          p2 = line2;
          for( c1 = *p1, c2 = *p2; c1 != '\0' && c2 != '\0'; ++p1, ++p2 )
          {
          if ( c1 != c2 )
          {
          match = 0;
          print_line( begin_line1 );
          printf("\n-----------------------\n"); print_line( begin_line2
          ); }
          }
          }
          }
          >
          >
          >
          void print_line( const char* line )
          {
          while( *line != '\0')
          {
          puts(*line++);
          }
          }
          >
          =============== = OUTPUT =============== ========
          >
          [arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
          7-6.c: In function `print_line':
          7-6.c:96: warning: passing arg 1 of `puts' makes pointer from integer
          without a cast
          What does that warning tell you?
          [arnuld@raj C]$ ./a.out 7-6.c replace-blanks.c Segmentationfau lt
          [arnuld@raj C]$
          You lied to your compiler (and he spotted it and warned you about it) and he
          got his revenge...

          Bye, Jojo


          Comment

          • arnuld

            #6
            Re: K&amp;R2 , exercise 7.6

            On Mon, 21 Apr 2008 11:35:00 +0100, Chris Dollin wrote:

            Ask yourself: what does `puts` accept as an parameter? What is
            the type of the expression `*line++`?

            K&R2 Appendix B, page 247:

            "int puts( const char *s )

            puts writes the string s and the newline to stdout. It returns EOF
            if an error occurs, non-negative otherwise"


            Now there are no strings in C. We have arrays of characters. It is same
            like printf() which accepts %s as an array of chars. So <lineis a single
            line of input form file stored into an array of chars.

            Are C characters a kind
            of integer? Do you really expect an integer to be freely and
            automatically converted to an integer?
            that was my mistake, my typo(s), I think I am getting sleepy. I just
            recently shifted from North-India to South-India and I had to change my
            food-habits. I am not able to get any food I used to eat from last 27
            years (since I was born). So I have lots of difficulties in living a
            normal daily routine here.




            --

            my email ID is at the above address

            Comment

            • Richard Heathfield

              #7
              Re: K&amp;R2 , exercise 7.6

              arnuld said:
              >On Mon, 21 Apr 2008 11:35:00 +0100, Chris Dollin wrote:
              >
              >
              >Ask yourself: what does `puts` accept as an parameter? What is
              >the type of the expression `*line++`?
              >
              >
              K&R2 Appendix B, page 247:
              >
              "int puts( const char *s )
              >
              puts writes the string s and the newline to stdout. It returns
              EOF
              if an error occurs, non-negative otherwise"
              I don't know whether you noticed, but I gave you a working version of
              print_line - and you appear to have broken it again.
              >
              >
              Now there are no strings in C.
              Wrong. In C, a string is a contiguous sequence of characters, terminated by
              the first null character.
              We have arrays of characters. It is same
              like printf() which accepts %s as an array of chars.
              Wrong. printf interprets %s as meaning "the parameter matching this format
              specifier is a ##pointer## to the first character in a contiguous sequence
              of characters that is terminated by a null character" - in other words, %s
              means "I'm giving you a string".

              <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

              • arnuld

                #8
                Re: K&amp;R2 , exercise 7.6

                On Mon, 21 Apr 2008 11:57:59 +0000, Richard Heathfield wrote:

                I don't know whether you noticed, but I gave you a working version of
                print_line - and you appear to have broken it again.

                yes. I fixed it again but I am using putchar() now.


                Wrong. In C, a string is a contiguous sequence of characters, terminated
                by the first null character.
                but these 2 are different:

                char arr[] = "Richard";
                char* pc = "Richard";

                both are contiguous sequence of characters ended by '\0'.

                Wrong. printf interprets %s as meaning "the parameter matching this
                format specifier is a ##pointer## to the first character in a contiguous
                sequence of characters that is terminated by a null character" - in
                other words, %s means "I'm giving you a string".

                you can never use the pc for %s in printf(). Thats what I meant, there
                are no strings, only arrays of chars. I tried to print the pc but all I
                got was:


                ^A^M^D @#





                --

                my email ID is at the above address

                Comment

                • arnuld

                  #9
                  Re: K&amp;R2 , exercise 7.6

                  On Mon, 21 Apr 2008 10:02:16 +0000, Richard Heathfield wrote:
                  >arnuld wrote:
                  > int c1, c2, match;
                  > char *line1, *line2;
                  Where do these point? (Hint: nowhere in particular.)

                  okay, here is the 3rd version of the program. It compiles and runs, the
                  only thing is semantic-bug, it always does one thing, no matter whether 2
                  files are same or different:



                  /* K&R2, section 7.7, exercise 7.6
                  *
                  * write a program to compare 2 files, printing that first line
                  * where they differ.
                  *
                  * version 1.2
                  */


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

                  enum MAXSIZE { ARRSIZE=1000 };

                  void compare_files( FILE*, FILE* );
                  void print_line( const char* );


                  int main( int argc, char* argv[] )
                  {
                  FILE *pf1, *pf2;

                  if( argc !=3 )
                  {
                  fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
                  exit(EXIT_FAILU RE);
                  }


                  /* open files */
                  pf1 = fopen( *++argv, "r" );
                  pf2 = fopen( *++argv, "r" );

                  /* error check */
                  if( pf1 == NULL || pf2 == NULL )
                  {
                  fprintf( stderr, "error opening files\n");
                  exit(EXIT_FAILU RE);
                  }
                  else
                  {
                  compare_files( pf1,pf2 );
                  }



                  /* don't forget to close the files */
                  fclose( pf1 );
                  fclose( pf2 );


                  return 0;
                  }



                  /* compare 2 files */
                  void compare_files( FILE* pf1, FILE* pf2 )
                  {
                  int match;
                  char c1, c2;
                  char line1[ARRSIZE];
                  char line2[ARRSIZE];
                  const char *begin_line1, *begin_line2, *p1, *p2;


                  match = 1;

                  while( (fgets( line1, ARRSIZE, pf1 ) != NULL) || (fgets( line2, ARRSIZE, pf2 ) != NULL))
                  {
                  begin_line1 = line1;
                  begin_line2 = line2;

                  p1 = line1;
                  p2 = line2;
                  for( c1 = *p1, c2 = *p2; c1 != '\0' && c2 != '\0'; ++p1, ++p2 )
                  {
                  if ( c1 != c2 )
                  {
                  match = 0;
                  print_line( begin_line1 );
                  printf("\n-----------------------\n");
                  print_line( begin_line2 );
                  return;
                  }
                  }
                  }
                  }



                  void print_line( const char* line )
                  {
                  while( *line != '\0')
                  {
                  putchar(*line++ );
                  }
                  }





                  --

                  my email ID is at the above address

                  Comment

                  • Richard Tobin

                    #10
                    Re: K&amp;R2 , exercise 7.6

                    In article <pan.2008.04.21 .17.39.57.67062 6@NoPain.Rome>,
                    arnuld <NoSpam@NoPain. Romewrote:
                    >Wrong. In C, a string is a contiguous sequence of characters, terminated
                    >by the first null character.
                    >but these 2 are different:
                    >
                    char arr[] = "Richard";
                    char* pc = "Richard";
                    >
                    >both are contiguous sequence of characters ended by '\0'.
                    And they are both strings.
                    >Wrong. printf interprets %s as meaning "the parameter matching this
                    >format specifier is a ##pointer## to the first character in a contiguous
                    >sequence of characters that is terminated by a null character" - in
                    >other words, %s means "I'm giving you a string".
                    >you can never use the pc for %s in printf().
                    What do you mean by "the pc"? Do you mean the variable pc declared
                    above? If so, you can certainly print it with %s.

                    -- Richard
                    --
                    :wq

                    Comment

                    • Joachim Schmitz

                      #11
                      Re: K&amp;R2 , exercise 7.6

                      arnuld wrote:
                      >On Mon, 21 Apr 2008 11:57:59 +0000, Richard Heathfield wrote:
                      >Wrong. In C, a string is a contiguous sequence of characters,
                      >terminated by the first null character.
                      >
                      but these 2 are different:
                      >
                      char arr[] = "Richard";
                      char* pc = "Richard";
                      >
                      both are contiguous sequence of characters ended by '\0'.
                      pc is a pointer to a (read-only) string
                      arr is an array containing a (modifyable) string
                      >Wrong. printf interprets %s as meaning "the parameter matching this
                      >format specifier is a ##pointer## to the first character in a
                      >contiguous sequence of characters that is terminated by a null
                      >character" - in other words, %s means "I'm giving you a string".
                      >
                      >
                      you can never use the pc for %s in printf().
                      Nonsense. In both cases printf only sees a pointer to char and prints that
                      up to the terminating \0.
                      Thats what I meant, there
                      are no strings, only arrays of chars. I tried to print the pc but all
                      I got was:
                      >
                      >
                      ^A^M^D @#
                      They you must have done something severly wrong.

                      Bye, Jojo


                      Comment

                      • Ben Bacarisse

                        #12
                        Re: K&amp;R2 , exercise 7.6

                        arnuld <NoSpam@NoPain. Romewrites:
                        okay, here is the 3rd version of the program. It compiles and runs, the
                        only thing is semantic-bug, it always does one thing, no matter whether 2
                        files are same or different:
                        <snip>
                        /* compare 2 files */
                        void compare_files( FILE* pf1, FILE* pf2 )
                        {
                        int match;
                        char c1, c2;
                        char line1[ARRSIZE];
                        char line2[ARRSIZE];
                        const char *begin_line1, *begin_line2, *p1, *p2;
                        >
                        >
                        match = 1;
                        What is this for?
                        while( (fgets( line1, ARRSIZE, pf1 ) != NULL) || (fgets( line2,
                        ARRSIZE, pf2 ) != NULL))
                        The logic here is wrong. || is a "a short-circuit" operator. Once
                        the first half gets a line the second half will not be executed. You
                        must read pairs of lines and I'd stop when one of the files runs out.
                        {
                        begin_line1 = line1;
                        begin_line2 = line2;
                        >
                        p1 = line1;
                        p2 = line2;
                        for( c1 = *p1, c2 = *p2; c1 != '\0' && c2 != '\0'; ++p1, ++p2 )
                        {
                        if ( c1 != c2 )
                        Why so many variables? What is wrong with strcmp?
                        {
                        match = 0;
                        print_line( begin_line1 );
                        printf("\n-----------------------\n");
                        print_line( begin_line2 );
                        return;
                        }
                        }
                        }
                        }
                        >
                        >
                        >
                        void print_line( const char* line )
                        {
                        while( *line != '\0')
                        {
                        putchar(*line++ );
                        }
                        }
                        What is the point of this function? It is just fputs, in effect.

                        --
                        Ben.

                        Comment

                        • arnuld

                          #13
                          Re: K&amp;R2 , exercise 7.6

                          On Mon, 21 Apr 2008 13:56:44 +0100, Ben Bacarisse wrote:
                          >arnuld wrote:
                          What is this for?
                          :-\


                          > while( (fgets( line1, ARRSIZE, pf1 ) != NULL) || (fgets( line2,
                          > ARRSIZE, pf2 ) != NULL))
                          The logic here is wrong. || is a "a short-circuit" operator. Once
                          the first half gets a line the second half will not be executed. You
                          must read pairs of lines and I'd stop when one of the files runs out.
                          As per my slowly developing C knowledge :P, If one line ends before ther
                          other then of course, the lines are not equal. This is true when either
                          both or any one of them runs out.

                          may be I did not understand your logic here but right now this is the
                          limit of my thinking.



                          What is the point of this function? It is just fputs, in effect.

                          K&R2 never talked about using it. So I never came across it.



                          Here is the 4th revision of the program. As usual output is not what I
                          wanted :(



                          /* K&R2, section 7.7, exercise 7.6
                          *
                          * write a program to compare 2 files, printing that first line
                          * where they differ.
                          *
                          * version 1.3
                          */


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

                          enum MAXSIZE { ARRSIZE=1000 };

                          void compare_files( FILE*, FILE* );


                          int main( int argc, char* argv[] )
                          {
                          FILE *pf1, *pf2;

                          if( argc !=3 )
                          {
                          fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
                          exit(EXIT_FAILU RE);
                          }


                          /* open files */
                          pf1 = fopen( *++argv, "r" );
                          pf2 = fopen( *++argv, "r" );

                          /* error check */
                          if( pf1 == NULL || pf2 == NULL )
                          {
                          fprintf( stderr, "error opening files\n");
                          exit(EXIT_FAILU RE);
                          }
                          else
                          {
                          compare_files( pf1,pf2 );
                          }



                          /* don't forget to close the files */
                          fclose( pf1 );
                          fclose( pf2 );


                          return 0;
                          }



                          /* compare 2 files */
                          void compare_files( FILE* pf1, FILE* pf2 )
                          {
                          char line1[ARRSIZE];
                          char line2[ARRSIZE];
                          const char *begin_line1, *begin_line2, *p1, *p2;


                          while( (fgets( line1, ARRSIZE, pf1 ) != NULL) ||
                          (fgets( line2, ARRSIZE, pf2 ) != NULL))
                          {
                          begin_line1 = p1 = line1;
                          begin_line2 = p2 = line2;

                          if( !strcmp( p1, p2 ) )
                          {
                          puts( begin_line1 );
                          printf("-------------- Lines differ here ---------------\n");
                          puts( begin_line2 );
                          return;
                          }
                          }
                          }
                          ============= OUTPUT =============== ==

                          [arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
                          [arnuld@raj C]$ ./a.out file1.txt file2.txt




                          -------------- Lines differ here ---------------




                          [arnuld@raj C]$













                          --

                          my email ID is at the above address

                          Comment

                          • Chris Dollin

                            #14
                            Re: K&amp;R2 , exercise 7.6

                            arnuld wrote:
                            >On Mon, 21 Apr 2008 11:35:00 +0100, Chris Dollin wrote:
                            >
                            >
                            >Ask yourself: what does `puts` accept as an parameter? What is
                            >the type of the expression `*line++`?
                            >
                            >
                            K&R2 Appendix B, page 247:
                            >
                            "int puts( const char *s )
                            >
                            puts writes the string s and the newline to stdout. It returns EOF
                            if an error occurs, non-negative otherwise"
                            >
                            Now there are no strings in C.
                            What makes you think that?
                            We have arrays of characters. It is same
                            like printf() which accepts %s as an array of chars. So <lineis a single
                            line of input form file stored into an array of chars.
                            `line` needn't have come from any of the program's input files:

                            puts( "I do not come from from this program's input files." );

                            --
                            "Thereafter , events may roll unheeded." /Foundation/

                            Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                            registered office: Berks RG12 1HN 690597 England

                            Comment

                            • edwin ng

                              #15
                              Re: K&amp;R2 , exercise 7.6

                              arnuld schreef:
                              >On Mon, 21 Apr 2008 13:56:44 +0100, Ben Bacarisse wrote:
                              >
                              >>arnuld wrote:
                              >
                              >
                              >
                              >> while( (fgets( line1, ARRSIZE, pf1 ) != NULL) || (fgets( line2,
                              >> ARRSIZE, pf2 ) != NULL))
                              >
                              >The logic here is wrong. || is a "a short-circuit" operator. Once
                              >the first half gets a line the second half will not be executed. You
                              >must read pairs of lines and I'd stop when one of the files runs out.
                              [snip]
                              >
                              while( (fgets( line1, ARRSIZE, pf1 ) != NULL) ||
                              (fgets( line2, ARRSIZE, pf2 ) != NULL))
                              You are still not seeing it ?
                              If you read line1 with success you will NOT read line2.

                              || means OR, so short circuit will prevent you from evaluating the second part (after the || ) if the first is true
                              because the answer is already known.

                              You could try && instead and see if that fixes it.
                              Good luck,
                              Edwin

                              Comment

                              Working...