Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with sizeof in while loop reading a file

Reply
Thread Tools

problem with sizeof in while loop reading a file

 
 
Richard Tobin
Guest
Posts: n/a
 
      09-14-2007
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
lisp9000@gmail.com
Guest
Posts: n/a
 
      09-14-2007
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

 
Reply With Quote
 
 
 
 
lisp9000@gmail.com
Guest
Posts: n/a
 
      09-15-2007
On Sep 14, 7:41 pm, (E-Mail Removed) (Richard Tobin) wrote:
> In article <(E-Mail Removed) .com>,
>
> (E-Mail Removed) <(E-Mail Removed)> 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-15-2007
"(E-Mail Removed)" 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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-15-2007
On Sat, 15 Sep 2007 00:32:54 -0000, "(E-Mail Removed)"
<(E-Mail Removed)> wrote in comp.lang.c:

> On Sep 14, 7:41 pm, (E-Mail Removed) (Richard Tobin) wrote:
> > In article <(E-Mail Removed) .com>,
> >
> > (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
lisp9000@gmail.com
Guest
Posts: n/a
 
      09-15-2007
On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> 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

 
Reply With Quote
 
lisp9000@gmail.com
Guest
Posts: n/a
 
      09-15-2007
On Sep 14, 11:37 pm, Jack Klein <(E-Mail Removed)> 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

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-15-2007
(E-Mail Removed) wrote, On 15/09/07 07:52:
> On Sep 14, 11:37 pm, Jack Klein <(E-Mail Removed)> 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
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-15-2007
<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) ups.com...
> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> 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


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-15-2007
Joachim Schmitz wrote, On 15/09/07 10:05:
> <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed) ups.com...
>> On Sep 14, 9:19 pm, CBFalconer <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments