problem with sizeof in while loop reading a file

L

lisp9000

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
 
L

lisp9000

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


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
 
C

CBFalconer

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 said:
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.
 
J

Jack Klein

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
 
L

lisp9000

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

Oops said:
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
 
L

lisp9000

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
 
F

Flash Gordon

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


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.
 
J

Joachim Schmitz

On Sep 14, 9:19 pm, CBFalconer <[email protected]> wrote:

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
 
F

Flash Gordon

Joachim Schmitz wrote, On 15/09/07 10:05:
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.
 
C

Charlie Gordon

Joachim Schmitz said:
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.
 
J

Joachim Schmitz

Charlie Gordon said:
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
 
C

CBFalconer

.... snip ...


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.
 
B

Bart van Ingen Schenau

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 said:
Lisp 9000

Bart v Ingen Schenau
 
K

Keith Thompson

Joachim Schmitz said:
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.
 
R

Richard Tobin

Keith Thompson said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top