473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to write this?

Hi all,
I am new to C programming,
All I just need to know is how to write the
following in C,
if (open STDOUT){
close the STDOUT
}

Can anyone guide me
Thanks

Nov 15 '05 #1
15 1320
se********@yahoo.co.in wrote:
Hi all,
I am new to C programming,
All I just need to know is how to write the
following in C,
if (open STDOUT){
close the STDOUT
}

Can anyone guide me
Thanks

Just close it; the close won't do anything if it isn't open.

Robert
Nov 15 '05 #2
se********@yahoo.co.in writes:
All I just need to know is how to write the
following in C,
if (open STDOUT){
close the STDOUT
}


You might say what you are actually trying to do.
What you are proposing does not make much sense,
because stdout is already open when main() is called.
--
"...deficient support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Nov 15 '05 #3
In article <11**********************@g14g2000cwa.googlegroups .com>,
<se********@yahoo.co.in> wrote:
I am new to C programming,
All I just need to know is how to write the
following in C,
if (open STDOUT){
close the STDOUT
} Can anyone guide me


There is no equivilent in standard C itself: what you are asking
about is one of the very common implementation-dependant system
extensions.

C itself does not have STDOUT, only stdout -- and uppercase vs
lowercase *does* matter for this purpose.

C's stdout is a macro which evaluates to a FILE* -- a pointer to
a structure. The operation to close stdout is fclose(stdout).

There is no specific call in C to determine whether a particular
FILE* is open: you just try an operation on it and check to see
if the operation failed with a code indicating the file is closed.
Or for your purposes, as one of the posters pointed out, just
go ahead and close it: you won't bomb if it is already closed.
STDOUT on the other hand is not a FILE*; it is generally part of
an I/O extension in which STDOUT is a macro which evaluates to the
"underlying descriptor" number of C's FILE* stdout . File descriptor
numbers are not part of C itself; many operations with file descriptors
are formalized in POSIX.1 . The typical operation to close a
file associated with a file descriptor is close(). There is no
standardized specific call to find out whether a file descriptor is
open, but you can fstat() the descriptor and look to see whether
you received the error that indicates the descriptor is not open.
--
Look out, there are llamas!
Nov 15 '05 #4
Walter Roberson wrote:
[...]
There is no specific call in C to determine whether a particular
FILE* is open: you just try an operation on it and check to see
if the operation failed with a code indicating the file is closed.
Or for your purposes, as one of the posters pointed out, just
go ahead and close it: you won't bomb if it is already closed.


So, the following is well-defined, assuming you pass a valid FILE*?

int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 15 '05 #5

Kenneth Brody wrote:
Walter Roberson wrote:
[...]
There is no specific call in C to determine whether a particular
FILE* is open: you just try an operation on it and check to see
if the operation failed with a code indicating the file is closed.
Or for your purposes, as one of the posters pointed out, just
go ahead and close it: you won't bomb if it is already closed.

if it was already closed, a call to fopen() could return the same
pointer, and then the "test" fclose() will close the wrong one.
So, the following is well-defined, assuming you pass a valid FILE*?

int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}


if you did the above in a threaded environment, it could have
unintended side-effects.

Nov 15 '05 #6
On Wed, 07 Sep 2005 13:44:29 -0400, Kenneth Brody wrote:
Walter Roberson wrote:
[...]
There is no specific call in C to determine whether a particular
FILE* is open: you just try an operation on it and check to see
if the operation failed with a code indicating the file is closed.
Or for your purposes, as one of the posters pointed out, just
go ahead and close it: you won't bomb if it is already closed.


So, the following is well-defined, assuming you pass a valid FILE*?

int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}

[...]


Nope, the value of f is indeterminate after the first fclose, the second
fclose invokes undefined behavior.

Robert Gamble
Nov 15 '05 #7
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
On Wed, 07 Sep 2005 13:44:29 -0400, Kenneth Brody wrote:
So, the following is well-defined, assuming you pass a valid FILE*? int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}

Nope, the value of f is indeterminate after the first fclose, the second
fclose invokes undefined behavior.


The value of f cannot change as a result of fclose, as it is passed
by value. What f points -to- might change, of course.

The C standard says that FEOF is returned if an error is encountered,
and places no constraints about what kind of error that might be.

If one drops over to POSIX.1 then fclose() is defined in terms of
close() and write(), and close() is defined with a specific errno
if the descriptor is not open; so for POSIX.1 at least, fclose() of
something already closed is defined.
--
"Never install telephone wiring during a lightning storm." -- Linksys
Nov 15 '05 #8
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
On Wed, 07 Sep 2005 13:44:29 -0400, Kenneth Brody wrote:

So, the following is well-defined, assuming you pass a valid FILE*? int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}

Nope, the value of f is indeterminate after the first fclose, the second
fclose invokes undefined behavior.


The value of f cannot change as a result of fclose, as it is passed
by value. What f points -to- might change, of course.

The C standard says that FEOF is returned if an error is encountered,
and places no constraints about what kind of error that might be.

If one drops over to POSIX.1 then fclose() is defined in terms of
close() and write(), and close() is defined with a specific errno
if the descriptor is not open; so for POSIX.1 at least, fclose() of
something already closed is defined.


The C standard says:

A successful call to the fclose function causes the stream pointed
to by stream to be flushed and the associated file to be
closed. Any unwritten buffered data for the stream are delivered
to the host environment to be written to the file; any unread
buffered data are discarded. Whether or not the call succeeds, the
stream is disassociated from the file and any buffer set by the
setbuf or setvbuf function is disassociated from the stream (and
deallocated if it was automatically allocated).

It looks like the effect of fclose() on a file that's already been
closed isn't specified, which makes it undefined behavior.

And given a sequence like this:

fclose(foo);
bar = fopen("whatever");
fclose(foo);

it's possible (I think) that bar could point to a FILE object at the
same address as the one released by the first fclose(). The second
fclose() would then close bar.

--
Keith Thompson (The_Other_Keith) 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.
Nov 15 '05 #9
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
And given a sequence like this:

fclose(foo);
bar = fopen("whatever");
fclose(foo);

it's possible (I think) that bar could point to a FILE object at the
same address as the one released by the first fclose(). The second
fclose() would then close bar.


The description of freopen() says that it "attempts to close any file that
is associated with the specified stream". The use of "any" suggests that
the stream may already have been closed - compare fclose() which talks
about "the" file.

-- Richard

Nov 15 '05 #10
On Thu, 08 Sep 2005 10:18:21 +0000, Richard Tobin wrote:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
And given a sequence like this:

fclose(foo);
bar = fopen("whatever");
fclose(foo);

it's possible (I think) that bar could point to a FILE object at the
same address as the one released by the first fclose(). The second
fclose() would then close bar.
The description of freopen() says that it "attempts to close any file that
is associated with the specified stream". The use of "any" suggests that
the stream may already have been closed - compare fclose() which talks
about "the" file.


When a stream is closed it ceases to be a stream, like when a malloc()'d
object is freed it ceases to be an object.

freopen() requires that you pass it a pointer to valid stream; a stream
that has been closed doesn't meet that requirement and you get undefined
behaviour. The same is true for all functions that take a FILE * argument,
except that you can pass a null pointer to those that say explicitly that
you can (fflush). In particular passing a pointer to anything that a
proper open stream to fclose() invokes undefined behaviour.

Lawrence


closed isn't a stream
any more (loke an object that has been freed isn't an object any more).

-- Richard


Nov 15 '05 #11
Keith Thompson wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
On Wed, 07 Sep 2005 13:44:29 -0400, Kenneth Brody wrote:

So, the following is well-defined, assuming you pass a valid FILE*?

int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}

Nope, the value of f is indeterminate after the first fclose, the second
fclose invokes undefined behavior.


The value of f cannot change as a result of fclose, as it is passed
by value. What f points -to- might change, of course.

The C standard says that FEOF is returned if an error is encountered,
and places no constraints about what kind of error that might be.

If one drops over to POSIX.1 then fclose() is defined in terms of
close() and write(), and close() is defined with a specific errno
if the descriptor is not open; so for POSIX.1 at least, fclose() of
something already closed is defined.


The C standard says:

A successful call to the fclose function causes the stream pointed
to by stream to be flushed and the associated file to be
closed. Any unwritten buffered data for the stream are delivered
to the host environment to be written to the file; any unread
buffered data are discarded. Whether or not the call succeeds, the
stream is disassociated from the file and any buffer set by the
setbuf or setvbuf function is disassociated from the stream (and
deallocated if it was automatically allocated).

It looks like the effect of fclose() on a file that's already been
closed isn't specified, which makes it undefined behavior.

And given a sequence like this:

fclose(foo);
bar = fopen("whatever");
fclose(foo);

it's possible (I think) that bar could point to a FILE object at the
same address as the one released by the first fclose(). The second
fclose() would then close bar.


Or it might not, in which case when it deallocates any buffer it might
call free on a pointer that has already been freed thus causing anything
to happen and a good (IMHO) example of why the C standard leaves it
undefined.

POSIX is, of course, allowed to define what happens on POSIX systems
when you invoke some specific case that C leaves undefined, but as far
as C is concerned it is still undefined.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #12
On Thu, 08 Sep 2005 03:38:35 +0000, Walter Roberson wrote:
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
On Wed, 07 Sep 2005 13:44:29 -0400, Kenneth Brody wrote:
So, the following is well-defined, assuming you pass a valid FILE*? int my_fclose(FILE *f)
{
fclose(f);
return(fclose(f));
}

Nope, the value of f is indeterminate after the first fclose, the second
fclose invokes undefined behavior.


The value of f cannot change as a result of fclose, as it is passed
by value. What f points -to- might change, of course.


That is of course what I meant, thanks for the correction.
The C standard says that FEOF is returned if an error is encountered,
That's EOF, not FEOF.
and places no constraints about what kind of error that might be.
But it mandates that even in failure, the function disassociates the
stream from the file. If you are saying that the first fclose could fail
and the second one could succeed without invoking undefined behavior then
I'll have to disagree for now (the wording in the standard regarding
this is a little sticky, it may be possible to persuade me otherwise).
If one drops over to POSIX.1 then fclose() is defined in terms of
close() and write(), and close() is defined with a specific errno
if the descriptor is not open; so for POSIX.1 at least, fclose() of
something already closed is defined.


Your logic is flawed, just because fclose performs a close (actually, the
"equivalent" of close() according to the standard), that doesn't mean that
is all it does. It could very well free other resources associated with
the stream for example. In any case, POSIX.1 states "After the call to
fclose(), any use of stream results in undefined behavior". I hope
you'll agree that calling fclose on a stream qualifies as a use of
that stream. This appears to include the case where fclose fails.

Robert Gamble
Nov 15 '05 #13
In article <pa***********************@netactive.co.uk>,
Lawrence Kirby <lk****@netactive.co.uk> wrote:
The description of freopen() says that it "attempts to close any file that
is associated with the specified stream". The use of "any" suggests that
the stream may already have been closed - compare fclose() which talks
about "the" file.
When a stream is closed it ceases to be a stream, like when a malloc()'d
object is freed it ceases to be an object.


A good theory, but it doesn't explain why freopen() says "any" instead
of "the".

-- Richard
Nov 15 '05 #14

In article <df**********@canopus.cc.umanitoba.ca>, ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <pa****************************@gmail.com>,
Robert Gamble <rg*******@gmail.com> wrote:
Nope, the value of f is indeterminate after the first fclose, the second
fclose invokes undefined behavior.
The value of f cannot change as a result of fclose, as it is passed
by value. What f points -to- might change, of course.


I believe the same principles apply here as with free():

The representation of the value of f cannot change (maybe; I'm not
sure anyone ever definitively established that for free), but the
interpretation of that value can. For example, if the implemen-
tation of fopen returns a malloc'd pointer, and fclose frees that
pointer, then any reference to the value of f after a successful
fclose invokes undefined behavior.
If one drops over to POSIX.1 then fclose() is defined in terms of
close() and write(), and close() is defined with a specific errno
if the descriptor is not open; so for POSIX.1 at least, fclose() of
something already closed is defined.


Not if fclose() frees the FILE*. The fclose implementation may
never reach the call to close().

--
Michael Wojcik mi************@microfocus.com

The guy who's fast in the mountain pass is the coolest.
-- _Initial D: Second Stage_
Nov 15 '05 #15
On Wed, 7 Sep 2005 17:21:14 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
<snip>
There is no specific call in C to determine whether a particular
FILE* is open: you just try an operation on it and check to see
if the operation failed with a code indicating the file is closed.
Or for your purposes, as one of the posters pointed out, just
go ahead and close it: you won't bomb if it is already closed.
As others have already said, there is no such guarantee in C; the
memory to which a FILE * points can be deallocated on fclose() and any
use of it (or formally even of the pointer) is undefined and can fail.

<OT> Even in SUS2 and 3, which are what I have to hand, [EBADF]
says "The file descriptor underlying stream is not valid." Note,
nothing about the _stream_ not being valid.

In SUS/POSIX, if you only close() the underlying fd but _not_ fclose()
the stdio stream, then at least operations that actually try to use
the underlying file seem guaranteed to reject with EBADF. </>

STDOUT on the other hand is not a FILE*; it is generally part of
an I/O extension in which STDOUT is a macro which evaluates to the
"underlying descriptor" number of C's FILE* stdout . File descriptor
numbers are not part of C itself; many operations with file descriptors
are formalized in POSIX.1 . The typical operation to close a
file associated with a file descriptor is close(). There is no
standardized specific call to find out whether a file descriptor is
open, but you can fstat() the descriptor and look to see whether
you received the error that indicates the descriptor is not open.


Or fcntl(,F_GETFL). Or lseek(,0,SEEK_CUR). The "low-level" I/O
operations are guaranteed to fail cleanly, no undefined behavior.

Note however that file descriptors can be and almost always are
reused. (Stdio FILE *'s also _can_ be reused, but only sometimes are.)
If you have a stale fd-number and test it before doing I/O, it may
have been reused by an open() (or socket() etc.) elsewhere in the
program, so your test mistakenly thinks it is "still open" but your
I/O does something almost certainly wrong. There is no adequate
alternative to remembering for yourself which files you have open.

- David.Thompson1 at worldnet.att.net
Nov 15 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and...
33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
10
by: Greg Hurlman | last post by:
I've got what I'm sure is a very simple problem. In an ASP page, I am trying to write out 4 fields from a recordset in succession: Response.Write rs("LastName") Response.Write rs("Suffix")...
1
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
3
by: Ike | last post by:
Can anyone discern why the following code writes the document.write() lines literally? That is, a line like document.write('<CENTER>') should write <CENTER> but instead writes the entire ...
2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
0
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
3
by: Brett_A | last post by:
I'm trying to write data from a form using a text box (textarea) that has a return after each item. For example: email1@domain.com email2@domain.com email3@domain.com email4@domain.com I'm...
1
by: anupamaavadutha | last post by:
hi all, iam new to javascript. i have problem calling javascript functions.iam designing a calender page.here is my code. <%@ page...
8
by: Mateusz Viste | last post by:
Hi, I am trying make some multimedia files playable from my website. So far, I am able to generate dynamically a new page containing the right <embed> section. However, when I load my script, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.