problem with sizeof in while loop reading a file

Discussion in 'C Programming' started by Richard Tobin, Sep 15, 2007.

  1. In article <>,
    <> wrote:

    > while ( (fgets(s, sizeof(s), fp)) != NULL)


    Here you call fgets, and don't print the result.

    > {
    > printf("about to call fgets\n");
    > fgets(s,sizeof(s),fp);


    Here you call it again.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 15, 2007
    #1
    1. Advertising

  2. Richard Tobin

    Guest

    I wrote a small test program to read a file of data and print each
    line, but it's only printing the 2nd line out of 3 total lines.

    The test file, "foo.txt", has 3 lines:

    7388: Zn->Z0 Run coward!
    8473: Q1->P2 HAHAHAHAHA!
    8381: G3->EVERYONE ok ok ok!

    But when I run my progam it only prints the second line:

    about to open file for reading
    testing if fp stream was opened
    entering while loop
    about to call fgets
    about to call printf
    8473: Q1->P2 HAHAHAHAHA!

    about to call fgets
    about to call printf


    closing file
    about to exit

    Also what does this warning mean and how can I eliminate it:
    foo.c: In function 'main':
    foo.c:33: warning: incompatible implicit declaration of built-in
    function 'exit'

    Here is my code:

    #include <stdio.h>

    int main(void)
    {

    FILE *fp;
    char s[80];

    printf("about to open file for reading\n");
    fp = fopen("foo.txt","r");
    printf("testing if fp stream was opened\n");
    if (fp != NULL)
    {

    printf("entering while loop\n");

    while ( (fgets(s, sizeof(s), fp)) != NULL)
    {
    printf("about to call fgets\n");
    fgets(s,sizeof(s),fp);
    printf("about to call printf\n");
    printf("%s\n",s);
    }
    }

    printf("closing file\n");

    fclose(fp);

    printf("about to exit\n");

    exit(0);

    }

    Lisp 9000
     
    , Sep 15, 2007
    #2
    1. Advertising

  3. Richard Tobin

    Guest

    On Sep 14, 7:41 pm, (Richard Tobin) wrote:
    > In article <>,
    >
    > <> wrote:
    > > while ( (fgets(s, sizeof(s), fp)) != NULL)

    >
    > Here you call fgets, and don't print the result.
    >
    > > {
    > > printf("about to call fgets\n");
    > > fgets(s,sizeof(s),fp);

    >
    > Here you call it again.


    Oops. Thanks Richard. And what does this warning mean and how can I
    eliminate it:
    foo.c: In function 'main':
    foo.c:33: warning: incompatible implicit declaration of built-in
    function 'exit'

    Lisp 9000
     
    , Sep 15, 2007
    #3
  4. Richard Tobin

    CBFalconer Guest

    "" wrote:
    >
    > I wrote a small test program to read a file of data and print each
    > line, but it's only printing the 2nd line out of 3 total lines.
    >
    > The test file, "foo.txt", has 3 lines:
    >
    > 7388: Zn->Z0 Run coward!
    > 8473: Q1->P2 HAHAHAHAHA!
    > 8381: G3->EVERYONE ok ok ok!
    >
    > But when I run my progam it only prints the second line:

    .... snip ...
    >
    > Here is my code:
    >
    > #include <stdio.h>


    Where is #include <stdlib.h> (for exit function)

    >
    > int main(void) {
    > FILE *fp;
    > char s[80];
    >
    > printf("about to open file for reading\n");
    > fp = fopen("foo.txt","r");
    > printf("testing if fp stream was opened\n");
    > if (fp != NULL) {
    > printf("entering while loop\n");
    > while ( (fgets(s, sizeof(s), fp)) != NULL) {
    > printf("about to call fgets\n");


    You just called fgets. eliminate this line.

    > fgets(s,sizeof(s),fp);


    And this line. It just discards the line just read.

    > printf("about to call printf\n");
    > printf("%s\n",s);
    > }
    > }
    > printf("closing file\n");
    > fclose(fp);
    > printf("about to exit\n");
    > exit(0);


    You could use "return 0" here in place of exit.

    > }


    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 15, 2007
    #4
  5. Richard Tobin

    Jack Klein Guest

    On Sat, 15 Sep 2007 00:32:54 -0000, ""
    <> wrote in comp.lang.c:

    > On Sep 14, 7:41 pm, (Richard Tobin) wrote:
    > > In article <>,
    > >
    > > <> wrote:
    > > > while ( (fgets(s, sizeof(s), fp)) != NULL)

    > >
    > > Here you call fgets, and don't print the result.
    > >
    > > > {
    > > > printf("about to call fgets\n");
    > > > fgets(s,sizeof(s),fp);

    > >
    > > Here you call it again.

    >
    > Oops. Thanks Richard. And what does this warning mean and how can I
    > eliminate it:
    > foo.c: In function 'main':
    > foo.c:33: warning: incompatible implicit declaration of built-in
    > function 'exit'


    It means that you should include the standard header that prototypes
    the exit() function, namely <stdlib.h>.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 15, 2007
    #5
  6. Richard Tobin

    Guest

    On Sep 14, 9:19 pm, CBFalconer <> wrote:
    >
    > Where is #include <stdlib.h> (for exit function)


    Oops, thought that was provided by <stdio.h> :)

    > You just called fgets. eliminate this line.

    ....
    > And this line. It just discards the line just read.


    Ah I see that now.

    > You could use "return 0" here in place of exit.


    I was curious about that. How do you know when to use "return 0;" vs
    "exit(0);" and does it make a difference?

    Lisp 9000
     
    , Sep 15, 2007
    #6
  7. Richard Tobin

    Guest

    On Sep 14, 11:37 pm, Jack Klein <> wrote:
    >
    > It means that you should include the standard header that prototypes
    > the exit() function, namely <stdlib.h>.


    I'll keep that in mind next time. Thanks Jack.

    > comp.lang.chttp://c-faq.com/


    Did you write the C FAQ? The ASCII version on the website hasn't been
    updated in 3 years. Is the FAQ static or will further updates be made?
    Just curious.

    Lisp 9000
     
    , Sep 15, 2007
    #7
  8. Richard Tobin

    Flash Gordon Guest

    wrote, On 15/09/07 07:52:
    > On Sep 14, 11:37 pm, Jack Klein <> wrote:
    >> It means that you should include the standard header that prototypes
    >> the exit() function, namely <stdlib.h>.

    >
    > I'll keep that in mind next time. Thanks Jack.
    >
    >> comp.lang.c http://c-faq.com/

    >
    > Did you write the C FAQ? The ASCII version on the website hasn't been
    > updated in 3 years. Is the FAQ static or will further updates be made?
    > Just curious.


    The authors details are on the site. The web version has been updated
    fairly recently, the other versions have not.
    --
    Flash Gordon
     
    Flash Gordon, Sep 15, 2007
    #8
  9. <> schrieb im Newsbeitrag
    news:...
    > On Sep 14, 9:19 pm, CBFalconer <> wrote:

    <snip>
    >> You could use "return 0" here in place of exit.

    >
    > I was curious about that. How do you know when to use "return 0;" vs
    > "exit(0);" and does it make a difference?

    Inside "main" (and inside main only) "return" is equvalent to "exit". Slight
    difference: "exit" is a function, so a propor prototy needs to be in scope
    and it needs the (), "return" is a keyword and the () are optional.

    Bye, Jojo
     
    Joachim Schmitz, Sep 15, 2007
    #9
  10. Richard Tobin

    Flash Gordon Guest

    Joachim Schmitz wrote, On 15/09/07 10:05:
    > <> schrieb im Newsbeitrag
    > news:...
    >> On Sep 14, 9:19 pm, CBFalconer <> wrote:

    > <snip>
    >>> You could use "return 0" here in place of exit.

    >> I was curious about that. How do you know when to use "return 0;" vs
    >> "exit(0);" and does it make a difference?

    > Inside "main" (and inside main only) "return" is equvalent to "exit".


    Unless main has been called recursively.

    > Slight
    > difference: "exit" is a function, so a propor prototy needs to be in scope
    > and it needs the (), "return" is a keyword and the () are optional.


    Personally I use return from main.
    --
    Flash Gordon
     
    Flash Gordon, Sep 15, 2007
    #10
  11. "Flash Gordon" <> schrieb im Newsbeitrag
    news:-gordon.me.uk...
    > Joachim Schmitz wrote, On 15/09/07 10:05:
    >> <> schrieb im Newsbeitrag
    >> news:...
    >>> On Sep 14, 9:19 pm, CBFalconer <> wrote:

    >> <snip>
    >>>> You could use "return 0" here in place of exit.
    >>> I was curious about that. How do you know when to use "return 0;" vs
    >>> "exit(0);" and does it make a difference?

    >> Inside "main" (and inside main only) "return" is equvalent to "exit".

    >
    > Unless main has been called recursively.

    Is that legal?

    Bye, Jojo
     
    Joachim Schmitz, Sep 15, 2007
    #11
  12. "Joachim Schmitz" <> a écrit dans le message
    de news: fcg79d$e5b$...
    > <> schrieb im Newsbeitrag
    > news:...
    >> On Sep 14, 9:19 pm, CBFalconer <> wrote:

    > <snip>
    >>> You could use "return 0" here in place of exit.

    >>
    >> I was curious about that. How do you know when to use "return 0;" vs
    >> "exit(0);" and does it make a difference?

    > Inside "main" (and inside main only) "return" is equvalent to "exit".
    > Slight difference: "exit" is a function, so a propor prototy needs to be
    > in scope and it needs the (), "return" is a keyword and the () are
    > optional.


    Even in main, exit and return are not equivalent:

    - the return statement merely transfers control back to the caller of
    function main with the value of the exit status. If main was called by the
    system runtime, it usually calls exit with the return value of main as if
    run-time code was exit(main(argc, argv)).

    - exit on the other hand is a function that does not return to its caller
    but terminates the program with an exit status (its argument). exit can be
    called from any point in the program resulting in immediate program
    termination, possibly preceded by various house-keeping tasks such as
    flushing stream buffers, closing files, releasing memory...

    - if the main function calls itself recursively, either directly or
    indirectly, whether it uses return or exit to finish its task will produce a
    very different outcome: return will allow the caller to continue, exit will
    cause the program to stop.

    --
    Chqrlie.
     
    Charlie Gordon, Sep 15, 2007
    #12
  13. "Charlie Gordon" <> schrieb im Newsbeitrag
    news:46ebc8b0$0$26295$...
    > "Joachim Schmitz" <> a écrit dans le message
    > de news: fcg79d$e5b$...
    >> <> schrieb im Newsbeitrag
    >> news:...
    >>> On Sep 14, 9:19 pm, CBFalconer <> wrote:

    >> <snip>
    >>>> You could use "return 0" here in place of exit.
    >>>
    >>> I was curious about that. How do you know when to use "return 0;" vs
    >>> "exit(0);" and does it make a difference?

    >> Inside "main" (and inside main only) "return" is equvalent to "exit".
    >> Slight difference: "exit" is a function, so a propor prototy needs to be
    >> in scope and it needs the (), "return" is a keyword and the () are
    >> optional.

    >
    > Even in main, exit and return are not equivalent:


    from n1256:

    5.1.2.2.3 Program termination

    1 If the return type of the main function is a type compatible with int, a
    return from the initial call to the main function is equivalent to calling
    the exit function with the value returned by the main function as its
    argument



    So the 'initial call to' part was missing in my post.

    Bye, Jojo
     
    Joachim Schmitz, Sep 15, 2007
    #13
  14. Richard Tobin

    santosh Guest

    Joachim Schmitz wrote:

    > "Flash Gordon" <> schrieb im Newsbeitrag
    > news:-gordon.me.uk...
    >> Joachim Schmitz wrote, On 15/09/07 10:05:
    >>> <> schrieb im Newsbeitrag
    >>> news:...
    >>>> On Sep 14, 9:19 pm, CBFalconer <> wrote:
    >>> <snip>
    >>>>> You could use "return 0" here in place of exit.
    >>>> I was curious about that. How do you know when to use "return 0;" vs
    >>>> "exit(0);" and does it make a difference?
    >>> Inside "main" (and inside main only) "return" is equvalent to "exit".

    >>
    >> Unless main has been called recursively.

    > Is that legal?


    Why shouldn't it be?
     
    santosh, Sep 15, 2007
    #14
  15. "santosh" <> schrieb im Newsbeitrag
    news:fcgtb6$ai6$...
    > Joachim Schmitz wrote:
    >
    >> "Flash Gordon" <> schrieb im Newsbeitrag
    >> news:-gordon.me.uk...
    >>> Joachim Schmitz wrote, On 15/09/07 10:05:
    >>>> <> schrieb im Newsbeitrag
    >>>> news:...
    >>>>> On Sep 14, 9:19 pm, CBFalconer <> wrote:
    >>>> <snip>
    >>>>>> You could use "return 0" here in place of exit.
    >>>>> I was curious about that. How do you know when to use "return 0;" vs
    >>>>> "exit(0);" and does it make a difference?
    >>>> Inside "main" (and inside main only) "return" is equvalent to "exit".
    >>>
    >>> Unless main has been called recursively.

    >> Is that legal?

    >
    > Why shouldn't it be?

    I though it might been forbidden by the standard. Apparently it is not.

    Bye, Jojo
     
    Joachim Schmitz, Sep 15, 2007
    #15
  16. Richard Tobin

    CBFalconer Guest

    "" wrote:
    > CBFalconer <> wrote:
    >

    .... snip ...
    >
    >> You could use "return 0" here in place of exit.

    >
    > I was curious about that. How do you know when to use "return 0;"
    > vs "exit(0);" and does it make a difference?


    Use return in main. exit exits the complete program, regardless of
    where called. Both can only take the arguments 0, EXIT_SUCCESS,
    EXIT_FAILURE. The macros are in stdlib.h.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 15, 2007
    #16
  17. Richard Tobin

    CBFalconer Guest

    Joachim Schmitz wrote:
    > "Flash Gordon" <> schrieb:
    >

    .... snip ...
    >
    >> Unless main has been called recursively.

    >
    > Is that legal?


    Yes

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 15, 2007
    #17
  18. wrote:

    > On Sep 14, 9:19 pm, CBFalconer <> wrote:
    >>
    >> You could use "return 0" here in place of exit.

    >
    > I was curious about that. How do you know when to use "return 0;" vs
    > "exit(0);" and does it make a difference?


    It will not really make a difference.
    The start-up code that gets executed right before main() might have the
    equivalent of
    exit(main(argc, argv));
    in it.

    The convention is to use 'return <status code>;' when you want to end
    the program from within the main() function, and 'exit(<status code>);'
    when you want to end the program from within another function.
    Some people will argue that program termination should only happen from
    the context of the main() function. (therefor, they won't ever use
    exit)

    The status codes that are guaranteed to have a meaning are
    - 0 for success
    - EXIT_SUCCESS for success (#include <stdlib.h>
    - EXIT_FAILURE for failure (#include <stdlib.h>

    >
    > Lisp 9000


    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    Bart van Ingen Schenau, Sep 15, 2007
    #18
  19. "Joachim Schmitz" <> writes:
    > "Charlie Gordon" <> schrieb im Newsbeitrag
    > news:46ebc8b0$0$26295$...
    >> "Joachim Schmitz" <> a écrit dans le message
    >> de news: fcg79d$e5b$...
    >>> <> schrieb im Newsbeitrag
    >>> news:...
    >>>> On Sep 14, 9:19 pm, CBFalconer <> wrote:
    >>> <snip>
    >>>>> You could use "return 0" here in place of exit.
    >>>>
    >>>> I was curious about that. How do you know when to use "return 0;" vs
    >>>> "exit(0);" and does it make a difference?
    >>> Inside "main" (and inside main only) "return" is equvalent to "exit".
    >>> Slight difference: "exit" is a function, so a propor prototy needs to be
    >>> in scope and it needs the (), "return" is a keyword and the () are
    >>> optional.

    >>
    >> Even in main, exit and return are not equivalent:

    >
    > from n1256:
    >
    > 5.1.2.2.3 Program termination
    >
    > 1 If the return type of the main function is a type compatible with int, a
    > return from the initial call to the main function is equivalent to calling
    > the exit function with the value returned by the main function as its
    > argument
    >
    > So the 'initial call to' part was missing in my post.


    That statement in the standard is very nearly correct. It's possible
    for 'exit(0);' and 'return 0;', executed in the initial call to main,
    to behave differently, but only in very obscure and perverse
    conditions.

    A function can be registered with 'atexit' so that it will be invoked
    when the program terminates. If you call exit() from main, such
    functions will be called before the main function terminates, so any
    automatically allocated objects within main will still exist. If you
    return from main, any registered functions will be called after the
    main function has terminated, and its local variables will have gone
    out of scope.

    Such a registered function cannot see main's local variables directly,
    but if the address of a variable that's local to main is saved in a
    file-scope pointer, then the registered function can see the variable,
    even though it can't refer to it by name.

    No sane programmer would do such a thing (other than as a test).

    Here's something even more obscure. If the implementation allows main
    to be declared as 'void main(void)', then you can register the main
    function itself with atexit(). I don't think this has any bizarre
    consequences; it just means main() will be called again.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 15, 2007
    #19
  20. In article <>,
    Keith Thompson <> wrote:

    >A function can be registered with 'atexit' so that it will be invoked
    >when the program terminates. If you call exit() from main, such
    >functions will be called before the main function terminates,


    Can you cite chapter and verse for this?

    According to C99 7.20.4.2, the registered functions are called at
    normal program termination. According to 7.20.4.3, exit() causes
    normal termination to occur. I don't see anything that prevents
    exit() from causing normal termination by longjmp()ing to main()'s
    caller, which would then handle the atexit() functions.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 15, 2007
    #20
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,290
    Tim Slattery
    Mar 30, 2005
  2. Derek
    Replies:
    7
    Views:
    24,417
    Ron Natalie
    Oct 14, 2004
  3. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    662
    CBFalconer
    Apr 10, 2004
  4. Uday Bidkar
    Replies:
    4
    Views:
    525
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    456
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page