Do you see anything wrong with programme ?

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

    Do you see anything wrong with programme ?

    I got the following programme from an old post:


    It was given at a job interview and the question was
    "The following program works, but what is a
    potential problem with it? "

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

    int main() {
    char buf[128];
    FILE *fp = fopen(__FILE__, "r");

    if (!fp) return EXIT_FAILURE;
    while(!feof(fp) ) {
    if (fgets(buf, sizeof buf, fp))
    puts(buf);
    }
    fclose(fp);
    return 0;
    }

    My opinion follows below but you might want
    to think about it before scrolling down.





















    The only problem I see with it is that it
    might insert some extra newlines relative
    to the original file. If the output of the
    programme is meant to be recompiled
    this might create problems. Otherwise
    it seems fine to me.

    Am I missing something ?
  • Eric Sosman

    #2
    Re: Do you see anything wrong with programme ?

    Spiros Bousbouras wrote:
    I got the following programme from an old post:

    >
    It was given at a job interview and the question was
    "The following program works, but what is a
    potential problem with it? "
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    int main() {
    char buf[128];
    FILE *fp = fopen(__FILE__, "r");
    >
    if (!fp) return EXIT_FAILURE;
    while(!feof(fp) ) {
    if (fgets(buf, sizeof buf, fp))
    puts(buf);
    }
    fclose(fp);
    return 0;
    }
    >
    My opinion follows below but you might want
    to think about it before scrolling down.
    >
    [...]
    >
    The only problem I see with it is that it
    might insert some extra newlines relative
    to the original file. If the output of the
    programme is meant to be recompiled
    this might create problems. Otherwise
    it seems fine to me.
    >
    Am I missing something ?
    A statement of what the program is supposed to do?
    We can see what it actually does, and we're told that it
    "works," but without knowing what it is supposed to do
    how can we tell if there's a "potential problem?" For
    example, maybe it's *intended* to split long lines.

    The only other things I notice is that the program
    tries to keep going after an input error and that it
    doesn't check for output errors -- but if these behaviors
    are part of the specification, they're not "problems."

    --
    Eric.Sosman@sun .com

    Comment

    • Richard Tobin

      #3
      Re: Do you see anything wrong with programme ?

      In article <d03ceec0-74df-4b60-8ef8-ab7431ffd514@a2 3g2000hsc.googl egroups.com>,
      Spiros Bousbouras <spibou@gmail.c omwrote:
      >It was given at a job interview and the question was
      >"The following program works, but what is a
      >potential problem with it? "
      >
      >#include <stdio.h>
      >#include <stdlib.h>
      >
      >int main() {
      char buf[128];
      FILE *fp = fopen(__FILE__, "r");
      >
      if (!fp) return EXIT_FAILURE;
      while(!feof(fp) ) {
      if (fgets(buf, sizeof buf, fp))
      puts(buf);
      }
      fclose(fp);
      return 0;
      >}
      >The only problem I see with it is that it
      >might insert some extra newlines relative
      >to the original file.
      It certainly inserts a blank line between every line, and others if
      any line exceeds the buffer size (which they don't if you're reading
      this file, but see below). fgets() goes more naturally with fputs(),
      and if you're not interpreting the text as lines it makes more sense
      to use fread() and fwrite().


      A more drastic problem is that __FILE__ is not guaranteed to refer
      to the right file at run-time. If the source has been deleted, or
      you're in a different directory, it's not going to work. It might
      even refer to a completely different file.

      -- Richard
      --
      :wq

      Comment

      • David Resnick

        #4
        Re: Do you see anything wrong with programme ?

        On May 15, 10:46 am, Spiros Bousbouras <spi...@gmail.c omwrote:
        I got the following programme from an old post:http://tinyurl.com/53oa6o
        >
        It was given at a job interview and the question was
        "The following program works, but what is a
        potential problem with it? "
        >
        #include <stdio.h>
        #include <stdlib.h>
        >
        int main() {
        char buf[128];
        FILE *fp = fopen(__FILE__, "r");
        >
        if (!fp) return EXIT_FAILURE;
        while(!feof(fp) ) {
        if (fgets(buf, sizeof buf, fp))
        puts(buf);
        }
        fclose(fp);
        return 0;
        >
        }
        >
        My opinion follows below but you might want
        to think about it before scrolling down.
        >
        The only problem I see with it is that it
        might insert some extra newlines relative
        to the original file. If the output of the
        programme is meant to be recompiled
        this might create problems. Otherwise
        it seems fine to me.
        >
        Am I missing something ?
        I don't do much file IO stuff, but fgets returns NULL on error. Is it
        possible for it to return NULL but not set end of file? If so,
        infinite loop...

        And yes, puts will double the number of newlines. And lines longer
        than
        buf (not in that source, but could be) will be broken into pieces.
        Seems
        like printf is called for here...

        -David

        Comment

        • Richard Tobin

          #5
          Re: Do you see anything wrong with programme ?

          In article <fba7f19e-e182-481b-b7a8-b9a9de6d3e6e@k3 7g2000hsf.googl egroups.com>,
          David Resnick <lndresnick@gma il.comwrote:
          >I don't do much file IO stuff, but fgets returns NULL on error. Is it
          >possible for it to return NULL but not set end of file?
          Yes, if it gets an i/o error instead.
          >If so, infinite loop...
          That depends on the nature of the error. I don't think the standard
          says anything about whether future reads may succeed after a error,
          or what they should read (e.g. should they retry to read the same
          block of a file?).

          -- Richard
          --
          :wq

          Comment

          Working...