R
Richard Tobin
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
while ( (fgets(s, sizeof(s), fp)) != NULL)
{
printf("about to call fgets\n");
fgets(s,sizeof(s),fp);
Here you call fgets, and don't print the result.
Here you call it again.
[email protected] said: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");
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);
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'
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.
You could use "return 0" here in place of exit.
It means that you should include the standard header that prototypes
the exit() function, namely <stdlib.h>.
comp.lang.chttp://c-faq.com/
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.
Inside "main" (and inside main only) "return" is equvalent to "exit". SlightOn 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.
Is that legal?Flash Gordon said:Joachim Schmitz wrote, On 15/09/07 10:05:
Unless main has been called recursively.
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.
Charlie Gordon said:Even in main, exit and return are not equivalent:
Joachim said:Is that legal?
I though it might been forbidden by the standard. Apparently it is not.santosh said:Why shouldn't it be?
[email protected] said:.... snip ...
I was curious about that. How do you know when to use "return 0;"
vs "exit(0);" and does it make a difference?
Joachim said:.... snip ...
Is that legal?
I was curious about that. How do you know when to use "return 0;" vs
"exit(0);" and does it make a difference?
- EXIT_FAILURE for failure (#include said:Lisp 9000
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.
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,
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.