how to open stdout

  • Follow


Assume stdout is closed and if i now
want to open stdout, how to open it?

IS this correct way of opening stdout

fopen(stdout,"/dev/null");


Thanks
Prasanna Bhat Mavinkuli

0
Reply boss_bhat (11) 9/8/2005 7:01:41 AM

<boss_bhat@yahoo.co.in> wrote in message
news:1126162198.682281.208370@z14g2000cwz.googlegroups.com...
> Assume stdout is closed and if i now
> want to open stdout, how to open it?

Don't close it. ("Doctor, when I do it it hurts.", "Don't do it.")

> IS this correct way of opening stdout
>
> fopen(stdout,"/dev/null");

Oh my goodness...
Alex


0
Reply alexfru (352) 9/8/2005 7:38:51 AM


please somebody explain me how to open STDOUT?

0
Reply boss_bhat (11) 9/8/2005 8:51:09 AM

<boss_bhat@yahoo.co.in> wrote in message
news:1126169469.388158.125330@g47g2000cwa.googlegroups.com...
> please somebody explain me how to open STDOUT?

It's already open upon entry to main(). Just don't close it and you'll be
OK.

Alex


0
Reply alexfru (352) 9/8/2005 9:35:02 AM

boss_bhat@yahoo.co.in wrote:
> Assume stdout is closed and if i now
> want to open stdout, how to open it?

Don't close it. If it is closed :

int fd;

fd = open("/dev/tty", O_WRONLY);
stdout = fdopen(fd, "w");

This code depends on UNIX, since closing / opening any of the standard 
streams is probably undefined :)

> IS this correct way of opening stdout
> 
> fopen(stdout,"/dev/null");

Depends. If you actually want to use data send to stdout : no. If you 
don't care : yes


	Igmar
0
Reply igmar3 (22) 9/8/2005 10:42:14 AM

boss_bhat@yahoo.co.in wrote:
# Assume stdout is closed and if i now
# want to open stdout, how to open it?
# 
# IS this correct way of opening stdout
# 
# fopen(stdout,"/dev/null");

Don't. Use
	freopen("/dev/null","w",stdout)
instead.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
0
Reply wyrmwif (945) 9/8/2005 12:46:45 PM

On Thu, 08 Sep 2005 00:01:41 -0700, boss_bhat wrote:

> Assume stdout is closed and if i now
> want to open stdout, how to open it?

If it is closed you can't portably reopen it in C.

> IS this correct way of opening stdout
> 
> fopen(stdout,"/dev/null");

No, fopen() takes a pointer to a string specifying a file name as its
first argument, and one specifying a mode as its second.

What you can do is change the file stdout is open to using the freopen()
function:

   freopen("/dev/null", "w", stdout)

The 3rd argument must be an existing open stream for this to be valid, so
just make sure you don't close stdout if you want to do this.

Lawrence

0
Reply lknews (877) 9/8/2005 12:47:39 PM

SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
>boss_bhat@yahoo.co.in wrote:
># Assume stdout is closed and if i now
># want to open stdout, how to open it?
># 
># IS this correct way of opening stdout
># 
># fopen(stdout,"/dev/null");
>
>Don't. Use
>	freopen("/dev/null","w",stdout)
>instead.

1. I know of several systems that don't know anything about /dev/null.

2. On the ones that do, writing to /dev/null is usually not the same 
   as writing to whatever-stdout-happened-to-point-to on program 
   startup.

3. Calling freopen with a third parameter that does not currently 
   point to a stream invokes undefined behaviour (AFAIK).

To OP: 
Simply don't close stdout, if you want to make further use of it. 

Best regards
-- 
Irrwahn Grausewitz (irrwahn35@freenet.de) 
welcome to clc      : http://www.ungerhu.com/jxh/clc.welcome.txt 
clc faq-list        : http://www.faqs.org/faqs/C-faq/faq/ 
clc frequent answers: http://benpfaff.org/writings/clc.
0
Reply irrwahn33 (608) 9/8/2005 1:06:40 PM

"Alexei A. Frounze" wrote:
> 
> <boss_bhat@yahoo.co.in> wrote in message
> news:1126169469.388158.125330@g47g2000cwa.googlegroups.com...
> > please somebody explain me how to open STDOUT?
> 
> It's already open upon entry to main(). Just don't close it and you'll be
> OK.

And if you need to associate stdout with something else for whatever
reason, check out freopen().

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


0
Reply kenbrody (1860) 9/8/2005 2:52:00 PM

Igmar Palsenberg wrote:
> 
> boss_bhat@yahoo.co.in wrote:
> > Assume stdout is closed and if i now
> > want to open stdout, how to open it?
> 
> Don't close it. If it is closed :
> 
> int fd;
> 
> fd = open("/dev/tty", O_WRONLY);
> stdout = fdopen(fd, "w");

stdout isn't an l-value.

[...]

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


0
Reply kenbrody (1860) 9/8/2005 2:54:55 PM

Kenneth Brody wrote:

>>Don't close it. If it is closed :
>>
>>int fd;
>>
>>fd = open("/dev/tty", O_WRONLY);
>>stdout = fdopen(fd, "w");
> 
> 
> stdout isn't an l-value.

Depends. On this system, it is. On systems where stdout is a macro, 
you're in trouble. If that's the case : stick with freopen()


	Igmar
0
Reply igmar3 (22) 9/9/2005 6:53:11 AM

Igmar Palsenberg wrote:
> 
> Kenneth Brody wrote:
> 
> >>Don't close it. If it is closed :
> >>
> >>int fd;
> >>
> >>fd = open("/dev/tty", O_WRONLY);
> >>stdout = fdopen(fd, "w");
> >
> >
> > stdout isn't an l-value.
> 
> Depends. On this system, it is. On systems where stdout is a macro,
> you're in trouble. If that's the case : stick with freopen()

"It may be an l-value on some systems" == "It's not a l-value as far as
clc is concerned".  :-)

Of course, one could make a similar argument about "/dev/tty".

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


0
Reply kenbrody (1860) 9/9/2005 2:31:25 PM

In article <43213155$0$11076$e4fe514c@news.xs4all.nl>,
Igmar Palsenberg  <igmar@jdimedia.local> wrote:
>Kenneth Brody wrote:

>> stdout isn't an l-value.

>Depends. On this system, it is. On systems where stdout is a macro, 
>you're in trouble. 

C89 -defines- stdout as being a macro.

It might happen that the expansion of the macro gives you something
that could be used as an l-value, but you shouldn't count on that.

A common Unix value for stdout is the local equivilent of  &__iob[1]
-- 
  "Who Leads?" / "The men who must... driven men, compelled men."
  "Freak men."
  "You're all freaks, sir. But you always have been freaks.
   Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
0
Reply roberson2 (8067) 9/9/2005 2:51:35 PM

12 Replies
63 Views

(page loaded in 3.398 seconds)


Reply: