understanding fgets()

Discussion in 'C Programming' started by arnuld, Jul 26, 2011.

  1. arnuld

    arnuld Guest

    I wrote this program which reads a file and prints it onto stdout
    using fgets().

    PROBLEM: fgets() reads line by line into an array. Lets say, in input
    file, first line is longer than 2nd line. Then after reading first
    line into array all characters read must be present in the array when
    it reads 2nd line e.g.

    1st line: usenet
    2nd line: clc

    after reading first line contents of array: usenet
    (array is not memset()ed)
    after reading second line contents of array: clc (why not clcnet)

    which means /use/ of /usenet/ was not replaced by /clc/ but contents
    of array were replaced/cleared. (slashes are used for readability
    here, emphasizing single words).

    QUESTION: My question is I did not clear array contents after one call
    to fgets() then who did ? fgets() ?



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

    enum { FGETS_READ_MAX = 256 };

    void print_file(FILE* p);

    int main(void)
    {
    const char* filename = "dp.conf";
    FILE* filep;

    filep = fopen(filename, "r");
    if(NULL == filep)
    {
    fprintf(stderr,"IN: %s @%d: ERROR opening file\n", __FILE__,
    __LINE__);
    return EXIT_FAILURE;
    }

    print_file(filep);

    if(fclose(filep))
    {
    printf("IN: %s @%d: ERROR closing file\n", __FILE__, __LINE__);
    }

    return 0;
    }



    void print_file(FILE* p)
    {
    char arrc[FGETS_READ_MAX + 2] = {0};

    while(fgets(arrc, FGETS_READ_MAX, p))
    {
    printf("\t::%s", arrc);
    }

    if(feof(p))
    {
    printf("Successful EoF EXIT :)\n");
    }
    else if(ferror(p))
    {
    printf("IN: %s @%d: ERROR reading file\n", __FILE__, __LINE__);
    }
    else
    {
    printf("OOPS! .. some other kind of issue ??\n");
    }

    }



    --
    www.lispmachine.wordpress.com
    find my email-ID @above blog
     
    arnuld, Jul 26, 2011
    #1
    1. Advertising

  2. On Jul 26, 8:53 am, arnuld <> wrote:

    > I wrote this program which reads a file and prints it onto stdout
    > using fgets().
    >
    > PROBLEM: fgets() reads line by line into an array. Lets say, in input
    > file, first line is longer than 2nd line. Then after reading first
    > line into array all characters read must be present in the array when
    > it reads 2nd line e.g.
    >
    > 1st line:  usenet
    > 2nd line:  clc
    >
    > after reading first line contents of array: usenet
    > (array is not memset()ed)
    > after reading second line contents of array: clc (why not clcnet)


    fgets terminates it's input with a nul char. So the array actually
    holds

    "clc\0et\0"

    printf(0ing the buffer stops at the first nul char

    <snip>
     
    Nick Keighley, Jul 26, 2011
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > On Jul 26, 1:05 pm, Nick Keighley <> wrote:

    > fgets terminates it's input with a nul char. So the array actually
    > holds
    >
    >   "clc\0et\0"
    > ..SNIP..


    Thanks Nick. I think I am taking a U-turn from a C hater -> C (not
    that good) -> C (brilliant) :)
     
    arnuld, Jul 26, 2011
    #3
  4. Nick Keighley <> writes:

    > On Jul 26, 8:53 am, arnuld <> wrote:
    >
    >> I wrote this program which reads a file and prints it onto stdout
    >> using fgets().
    >>
    >> PROBLEM: fgets() reads line by line into an array. Lets say, in input
    >> file, first line is longer than 2nd line. Then after reading first
    >> line into array all characters read must be present in the array when
    >> it reads 2nd line e.g.
    >>
    >> 1st line:  usenet
    >> 2nd line:  clc
    >>
    >> after reading first line contents of array: usenet
    >> (array is not memset()ed)
    >> after reading second line contents of array: clc (why not clcnet)

    >
    > fgets terminates it's input with a nul char. So the array actually
    > holds
    >
    > "clc\0et\0"


    A detail: fgets retains the newline (when there is room for it) so the
    first call most likely produced

    "usenet\n\0"

    and the second one resulted in

    "clc\n\0t\n\0"

    There has been some debate about whether fgets has permission to alter
    the array elements beyond the null that it is obliged to write.
    Whatever view you take on this point, it is unlikely that an
    implementation will actually do this and therefore the presence of
    "t\n\0" is a pretty sound bet.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Jul 26, 2011
    #4
  5. arnuld

    Chad Guest

    On Jul 26, 2:25 am, arnuld <> wrote:
    > > On Jul 26, 1:05 pm, Nick Keighley <> wrote:
    > > fgets terminates it's input with a nul char. So the array actually
    > > holds

    >
    > >   "clc\0et\0"
    > > ..SNIP..

    >
    > Thanks Nick. I think I am taking a U-turn from a C hater -> C (not
    > that good) -> C (brilliant) :)


    I guess it depends on what your doing. For example, say I want to
    write a program that calculates the factorial of 1000!. I can do this
    in line of Haskell and get the exact number. However, doing the exact
    same thing in C would require a lot more work.
     
    Chad, Jul 26, 2011
    #5
  6. arnuld

    Chad Guest

    On Jul 26, 9:12 am, Chad <> wrote:
    > On Jul 26, 2:25 am, arnuld <> wrote:
    >
    > > > On Jul 26, 1:05 pm, Nick Keighley <> wrote:
    > > > fgets terminates it's input with a nul char. So the array actually
    > > > holds

    >
    > > >   "clc\0et\0"
    > > > ..SNIP..

    >
    > > Thanks Nick. I think I am taking a U-turn from a C hater -> C (not
    > > that good) -> C (brilliant) :)

    >
    > I guess it depends on what your doing. For example, say I want to
    > write a program that calculates the factorial of 1000!. I can do this
    > in line of Haskell and get the exact number. However, doing the exact
    > same thing in C would require a lot more work.


    er *do this one line of Haskell*
     
    Chad, Jul 26, 2011
    #6
  7. arnuld

    Willem Guest

    Chad wrote:
    ) I guess it depends on what your doing. For example, say I want to
    ) write a program that calculates the factorial of 1000!. I can do this
    ) in line of Haskell and get the exact number. However, doing the exact
    ) same thing in C would require a lot more work.

    Indeed.
    You would have to locate and download a library for manipulating large
    numbers, and then write so many lines of code, perhaps even ten or more.

    Any more silly examples ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 26, 2011
    #7
  8. arnuld

    Chad Guest

    On Jul 26, 9:21 am, Willem <> wrote:
    > Chad wrote:
    >
    > ) I guess it depends on what your doing. For example, say I want to
    > ) write a program that calculates the factorial of 1000!. I can do this
    > ) in line of Haskell and get the exact number. However, doing the exact
    > ) same thing in C would require a lot more work.
    >
    > Indeed.
    > You would have to locate and download a library for manipulating large
    > numbers, and then write so many lines of code, perhaps even ten or more.
    >
    > Any more silly examples ?
    >
    >


    Nope.
     
    Chad, Jul 26, 2011
    #8
  9. arnuld

    Seebs Guest

    On 2011-07-26, arnuld <> wrote:
    > PROBLEM: fgets() reads line by line into an array. Lets say, in input
    > file, first line is longer than 2nd line. Then after reading first
    > line into array all characters read must be present in the array when
    > it reads 2nd line e.g.


    Huh?

    > 1st line: usenet
    > 2nd line: clc


    > after reading first line contents of array: usenet
    > (array is not memset()ed)
    > after reading second line contents of array: clc (why not clcnet)


    No.

    After reading first line, contents of array: "usenet\0".

    After reading second line, contents of array: "clc\0et\0".

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jul 26, 2011
    #9
  10. Seebs <> writes:

    > On 2011-07-26, arnuld <> wrote:
    >> PROBLEM: fgets() reads line by line into an array. Lets say, in input
    >> file, first line is longer than 2nd line. Then after reading first
    >> line into array all characters read must be present in the array when
    >> it reads 2nd line e.g.

    >
    > Huh?
    >
    >> 1st line: usenet
    >> 2nd line: clc

    >
    >> after reading first line contents of array: usenet
    >> (array is not memset()ed)
    >> after reading second line contents of array: clc (why not clcnet)

    >
    > No.
    >
    > After reading first line, contents of array: "usenet\0".
    >
    > After reading second line, contents of array: "clc\0et\0".


    You mean "usenet\n\0" then "clc\n\0t\n\0".

    --
    Ben.
     
    Ben Bacarisse, Jul 26, 2011
    #10
  11. arnuld

    Seebs Guest

    On 2011-07-26, Ben Bacarisse <> wrote:
    > Seebs <> writes:
    >> After reading first line, contents of array: "usenet\0".


    >> After reading second line, contents of array: "clc\0et\0".


    > You mean "usenet\n\0" then "clc\n\0t\n\0".


    A very charitable assumption, but no -- I meant what I said. I was just
    wrong. :)

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jul 26, 2011
    #11
  12. On Jul 26, 7:21 pm, Willem <> wrote:
    > Chad wrote:
    >
    > ) I guess it depends on what your doing. For example, say I want to
    > ) write a program that calculates the factorial of 1000!. I can do this
    > ) in line of Haskell and get the exact number. However, doing the exact
    > ) same thing in C would require a lot more work.
    >
    > Indeed.
    > You would have to locate and download a library for manipulating large
    > numbers, and then write so many lines of code, perhaps even ten or more.
    >
    > Any more silly examples ?
    >

    No, it's a fair point.

    High precision maths libraries don't ship as standard in C. So you
    have to locate a library, then install it, and that's often difficult.
    Most people actually find it easier to incorporate most libraries as
    source. There is often quite a bit of fiddling to do. Then it might
    have declared bool and necessitate a rewriting of your own source.

    It's also virtually impossible to read mathematical expressions when
    the arithmetical operators are replaced with function calls.
     
    Malcolm McLean, Jul 27, 2011
    #12
    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. Charles Wilkins

    cin before fgets

    Charles Wilkins, Aug 7, 2003, in forum: C++
    Replies:
    4
    Views:
    716
    Charles Wilkins
    Aug 7, 2003
  2. Mike Mimic

    fgets and newline

    Mike Mimic, May 15, 2004, in forum: C++
    Replies:
    4
    Views:
    8,020
    John Harrison
    May 15, 2004
  3. DJP
    Replies:
    7
    Views:
    7,376
    glen herrmannsfeldt
    Oct 21, 2004
  4. Replies:
    6
    Views:
    1,238
    Mark McIntyre
    Jun 25, 2003
  5. Eigenvector

    fgets and problems reading into array

    Eigenvector, Jul 26, 2003, in forum: C Programming
    Replies:
    12
    Views:
    630
    Glen Herrmannsfeldt
    Jul 29, 2003
Loading...

Share This Page