problem with sizeof in while loop reading a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lisp9000@gmail.com

    problem with sizeof in while loop reading a file

    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("enterin g 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

  • Richard Tobin

    #2
    Re: problem with sizeof in while loop reading a file

    In article <1189813623.280 906.180560@r29g 2000hsg.googleg roups.com>,
    lisp9000@gmail. com <lisp9000@gmail .comwrote:
    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
    --
    "Considerat ion shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.

    Comment

    • lisp9000@gmail.com

      #3
      Re: problem with sizeof in while loop reading a file

      On Sep 14, 7:41 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
      In article <1189813623.280 906.180...@r29g 2000hsg.googleg roups.com>,
      >
      lisp9...@gmail. com <lisp9...@gmail .comwrote:
      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

      Comment

      • CBFalconer

        #4
        Re: problem with sizeof in while loop reading a file

        "lisp9000@gmail .com" 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("enterin g 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

        Comment

        • Jack Klein

          #5
          Re: problem with sizeof in while loop reading a file

          On Sat, 15 Sep 2007 00:32:54 -0000, "lisp9000@gmail .com"
          <lisp9000@gmail .comwrote in comp.lang.c:
          On Sep 14, 7:41 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
          In article <1189813623.280 906.180...@r29g 2000hsg.googleg roups.com>,

          lisp9...@gmail. com <lisp9...@gmail .comwrote:
          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.l earn.c-c++

          Comment

          • lisp9000@gmail.com

            #6
            Re: problem with sizeof in while loop reading a file

            On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
            >
            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

            Comment

            • lisp9000@gmail.com

              #7
              Re: problem with sizeof in while loop reading a file

              On Sep 14, 11:37 pm, Jack Klein <jackkl...@spam cop.netwrote:
              >
              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

              Comment

              • Flash Gordon

                #8
                Re: problem with sizeof in while loop reading a file

                lisp9000@gmail. com wrote, On 15/09/07 07:52:
                On Sep 14, 11:37 pm, Jack Klein <jackkl...@spam cop.netwrote:
                >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

                Comment

                • Joachim Schmitz

                  #9
                  Re: problem with sizeof in while loop reading a file

                  <lisp9000@gmail .comschrieb im Newsbeitrag
                  news:1189839063 .776866.70200@n 39g2000hsh.goog legroups.com...
                  On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                  <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


                  Comment

                  • Flash Gordon

                    #10
                    Re: problem with sizeof in while loop reading a file

                    Joachim Schmitz wrote, On 15/09/07 10:05:
                    <lisp9000@gmail .comschrieb im Newsbeitrag
                    news:1189839063 .776866.70200@n 39g2000hsh.goog legroups.com...
                    >On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                    <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

                    Comment

                    • Joachim Schmitz

                      #11
                      Re: problem with sizeof in while loop reading a file

                      "Flash Gordon" <spam@flash-gordon.me.uksch rieb im Newsbeitrag
                      news:h2msr4xlql .ln2@news.flash-gordon.me.uk...
                      Joachim Schmitz wrote, On 15/09/07 10:05:
                      ><lisp9000@gmai l.comschrieb im Newsbeitrag
                      >news:118983906 3.776866.70200@ n39g2000hsh.goo glegroups.com.. .
                      >>On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                      ><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


                      Comment

                      • Charlie Gordon

                        #12
                        Re: problem with sizeof in while loop reading a file

                        "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dea écrit dans le message
                        de news: fcg79d$e5b$1@on line.de...
                        <lisp9000@gmail .comschrieb im Newsbeitrag
                        news:1189839063 .776866.70200@n 39g2000hsh.goog legroups.com...
                        >On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                        <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.


                        Comment

                        • Joachim Schmitz

                          #13
                          Re: problem with sizeof in while loop reading a file

                          "Charlie Gordon" <news@chqrlie.o rgschrieb im Newsbeitrag
                          news:46ebc8b0$0 $26295$426a74cc @news.free.fr.. .
                          "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dea écrit dans le message
                          de news: fcg79d$e5b$1@on line.de...
                          ><lisp9000@gmai l.comschrieb im Newsbeitrag
                          >news:118983906 3.776866.70200@ n39g2000hsh.goo glegroups.com.. .
                          >>On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                          ><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


                          Comment

                          • santosh

                            #14
                            Re: problem with sizeof in while loop reading a file

                            Joachim Schmitz wrote:
                            "Flash Gordon" <spam@flash-gordon.me.uksch rieb im Newsbeitrag
                            news:h2msr4xlql .ln2@news.flash-gordon.me.uk...
                            >Joachim Schmitz wrote, On 15/09/07 10:05:
                            >><lisp9000@gma il.comschrieb im Newsbeitrag
                            >>news:11898390 63.776866.70200 @n39g2000hsh.go oglegroups.com. ..
                            >>>On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                            >><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?

                            Comment

                            • Joachim Schmitz

                              #15
                              Re: problem with sizeof in while loop reading a file

                              "santosh" <santosh.k83@gm ail.comschrieb im Newsbeitrag
                              news:fcgtb6$ai6 $1@aioe.org...
                              Joachim Schmitz wrote:
                              >
                              >"Flash Gordon" <spam@flash-gordon.me.uksch rieb im Newsbeitrag
                              >news:h2msr4xlq l.ln2@news.flas h-gordon.me.uk...
                              >>Joachim Schmitz wrote, On 15/09/07 10:05:
                              >>><lisp9000@gm ail.comschrieb im Newsbeitrag
                              >>>news:1189839 063.776866.7020 0@n39g2000hsh.g ooglegroups.com ...
                              >>>>On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                              >>><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


                              Comment

                              Working...