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
Sep 14 '07
23 2095
"Flash Gordon" <sp**@flash-gordon.me.uksch rieb im Newsbeitrag
news:h2******** ****@news.flash-gordon.me.uk...
Joachim Schmitz wrote, On 15/09/07 10:05:
><li******@gmai l.comschrieb im Newsbeitrag news:11******* **************@ 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
"Joachim Schmitz" <no*********@sc hmitz-digital.dea écrit dans le message
de news: fc**********@on line.de...
<li******@gmail .comschrieb im Newsbeitrag
news:11******** *************@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.
"Charlie Gordon" <ne**@chqrlie.o rgschrieb im Newsbeitrag
news:46******** *************** @news.free.fr.. .
"Joachim Schmitz" <no*********@sc hmitz-digital.dea écrit dans le message
de news: fc**********@on line.de...
><li******@gmai l.comschrieb im Newsbeitrag news:11******* **************@ 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
Joachim Schmitz wrote:
"Flash Gordon" <sp**@flash-gordon.me.uksch rieb im Newsbeitrag
news:h2******** ****@news.flash-gordon.me.uk...
>Joachim Schmitz wrote, On 15/09/07 10:05:
>><li******@gma il.comschrieb im Newsbeitrag news:11****** *************** @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?
"santosh" <sa*********@gm ail.comschrieb im Newsbeitrag
news:fc******** **@aioe.org...
Joachim Schmitz wrote:
>"Flash Gordon" <sp**@flash-gordon.me.uksch rieb im Newsbeitrag news:h2******* *****@news.flas h-gordon.me.uk...
>>Joachim Schmitz wrote, On 15/09/07 10:05: <li******@gm ail.comschrieb im Newsbeitrag news:11***** *************** *@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
"li******@gmail .com" wrote:
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?
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
Joachim Schmitz wrote:
"Flash Gordon" <sp**@flash-gordon.me.uksch rieb:
.... 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 li******@gmail. com wrote:
On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>> 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/
"Joachim Schmitz" <no*********@sc hmitz-digital.dewrite s:
"Charlie Gordon" <ne**@chqrlie.o rgschrieb im Newsbeitrag
news:46******** *************** @news.free.fr.. .
>"Joachim Schmitz" <no*********@sc hmitz-digital.dea écrit dans le message de news: fc**********@on line.de...
>><li******@gma il.comschrieb im Newsbeitrag news:11****** *************** @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". 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_Keit h) ks***@mib.org <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"
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.orgw rote:
>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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: JasBascom |
last post by:
int main()
{
char record_typec = 'c';
record_typec = toupper(record_typec);
* rec->Newcrecord.record_type = record_typec;
etc
etc
\*********************************************\
|
by: ishmael4 |
last post by:
hello everyone!
i have a problem with reading from binary file. i was googling and
searching, but i just cant understand, why isnt this code working. i could
use any help. here's the source code:
--cut here--
typedef struct pkg_ {
short int info;
char* data;
|
by: dbuser |
last post by:
Hi,
I need help on a problem, as described below. I am reading a file
"input.txt"which has data like this:
abc def gh izk lmnopq rst uvwxyz
I am using fstream object to read the file and writing into a dynamic
array. My problem is that the array shows extra z and probably because
of this further processing gives run time error in borland compiler.
Can you please tell me, if the problem is related to handling end-of
line , how do i do...
|
by: Abhishek |
last post by:
I have a problem transfering files using sockets from pocket pc(.net compact
c#) to desktop(not using .net just mfc and sockets 2 API). The socket
communication is not a issue and I am able to transfer data across.On the
serve I am using Socket 2 API (recv function to read bytes)and not using
..NET. I use FileStream to open the file on the pocket pc, then associate a
BinaryReader object with the stream and call ReadBytes to read all the...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c */
#include <time.h>
| |
by: xoinki |
last post by:
hi experts,
I need a little help in debugging this code.. would u
pleeze kindly help me?
here this program sends a datagram every 10 seconds and on reception it
cheks whether the source IP is already written in file or not.. if it
is not already entered into the file then a file is opened and that IP
is entered.
the problem is with this part..
if the filewrite() function is tested independently it is
working but in this program...
|
by: Babak |
last post by:
Hi Everyone,
I've written a standard C code for a simple finite element analysis
in MSVC++ . When I save the file as a cpp file, it compiles and runs
perfectly, but when I save it as a c file, there are lots of errors and
warnings in compiling stage. I am really confused about this problem
because I'm not even familiar with C++ and my code only includes simple
C functions. Can anybody please tell me what's my mistake? Is there any
other...
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: xiao |
last post by:
It always dumped when I tried to run it... But it compiles OK. What I
want to do is to do a test:
Read information from a .dat file and then write it to another file.
The original DAT file is like this : (very simple..........)
010001010110001101010101010101010101010101
#include<stdio.h>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |