Do you see anything wrong with programme ?

Discussion in 'C Programming' started by Spiros Bousbouras, May 15, 2008.

  1. 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 ?
     
    Spiros Bousbouras, May 15, 2008
    #1
    1. Advertising

  2. In article <>,
    Spiros Bousbouras <> wrote:

    >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
     
    Richard Tobin, May 15, 2008
    #2
    1. Advertising

  3. On May 15, 10:46 am, Spiros Bousbouras <> wrote:
    > 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
     
    David Resnick, May 15, 2008
    #3
  4. In article <>,
    David Resnick <> wrote:

    >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
     
    Richard Tobin, May 15, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    3
    Views:
    979
    Tony Morris
    Feb 9, 2004
  2. John Gagon

    If you could add anything you want

    John Gagon, May 12, 2006, in forum: Java
    Replies:
    55
    Views:
    1,406
    Oliver Wong
    May 25, 2006
  3. Ever see anything like this?

    , Feb 21, 2006, in forum: C Programming
    Replies:
    4
    Views:
    323
  4. Jim Langston
    Replies:
    10
    Views:
    511
    Richard Herring
    Dec 4, 2007
  5. OldButStillLearning

    Now You See It Now You Don't

    OldButStillLearning, Dec 11, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    303
    Juan T. Llibre
    Dec 12, 2007
Loading...

Share This Page