473,800 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1351
se********@yaho o.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********@yaho o.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.
--
"...deficie nt support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Nov 15 '05 #3
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
<se********@yah oo.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*******@gmai l.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.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <pa************ *************** *@gmail.com>,
Robert Gamble <rg*******@gmai l.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_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.
Nov 15 '05 #9
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> 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

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

Similar topics

6
16620
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 fwrite() in C allow the user to specify the size of the data to be written. Python's write only allows a string to be passed in.
33
3498
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 write a program or a project of some sort, how do you actually start. Picture this, you know what you want the program to do (its features), you have a possably rough idea of how the program will work. You have opened an empty text file and saved...
10
2912
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") Response.Write rs("FirstName") Response.Write rs("MiddleInitial") All fields are populated and are single terms. I would expect "LastNameSuffixFirstNameMiddleInitial" ("UserIIITestA" in my test record) to
1
2875
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();'>"); document.write("<OPTION VALUE='0.345066110642241'>Argentina Peso </OPTION>"); document.write("<OPTION VALUE='0.790200069503053'>Australia Dollar
3
2524
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 document.write('<CENTER>'). I must be really doing something stupid. Can anyone here illuminate me? Thanks, Ike <HTML><HEAD> <TITLE>My Applet</TITLE> </HEAD><BODY>
2
2386
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 copying/pasting everytime I change it, I figure this will be better, as I only change it once. The problem is, document.write doesn't handle multiple lines very well, so I was wondering what is the best way to do this? Maybe there is even a better...
0
1763
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 correct, but giving error says " file is accessed by other process cannot access" I have some logic inbetween and write the info using response.write in to html format Ex: Response.Write("<td>" & "client_no" & "</td>"). I am getting the error at :...
3
3897
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 getting the data written to the SQL server table fine, but every item after the first is getting written with a special character
1
7547
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 import='java.io.*,java.util.*,java.sql.*,javax.servlet.*,javax.servlet.http.*' session='true'%> <%@ page import='java.text.*'%> <html> <script language='javascript'> function backward() { alert("hello"); }
8
7112
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 overwrites the current page. Is there any way I could load a new tab or window, and put the new content into it? Here is the script I am using right now:
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
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...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
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 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...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6813
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();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.