473,387 Members | 1,493 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,387 software developers and data experts.

Temporarily close stdout?

[ This question is about closing/reopening/... of stdout - I hope that
is on topic?]

Hello,

I have written a program in C; this programs uses an external
proprietary library. When calling a certain function in the external
library, the particular function writes a message to stdout. I am not
particularly interested in this message, and would like to silence it
- however I do not know how to do it. (I stdout and stderr my self, so
just redirecting into oblivion is not an option).

An excerpt of the code looks like this:
....
/* Offending call in external library. */
job_nr = lsb_submit( &request , &reply);
When this code is run - the offending function (or some function it
calls), writes
"Job <123456is submitted to queue <common>"

An information I am completely uninterested in sending stdout. So I
wondered if it would be possible to do something like this:
....
/* Temporarily close stdout: */
fclose(stdout);
job_nr = lsb_submit( &request , &reply );
/* Reopen stdout */
stdout = fdopen(1 , "a");

I have tried the code listed above - but the fdopen() failed with "Bad
file descriptor" (I had not expected it to succeed ...). Anyway - any
suggestions would be highly appreciated.
Joakim
Jun 27 '08 #1
5 8924
Joakim wrote:
) [ This question is about closing/reopening/... of stdout - I hope that
) is on topic?]
)
) Hello,
)
) I have written a program in C; this programs uses an external
) proprietary library. When calling a certain function in the external
) library, the particular function writes a message to stdout. I am not
) particularly interested in this message, and would like to silence it
) - however I do not know how to do it. (I stdout and stderr my self, so
) just redirecting into oblivion is not an option).
) ....
) /* Temporarily close stdout: */
) fclose(stdout);
) job_nr = lsb_submit( &request , &reply );
) /* Reopen stdout */
) stdout = fdopen(1 , "a");
)
) I have tried the code listed above - but the fdopen() failed with "Bad
) file descriptor" (I had not expected it to succeed ...). Anyway - any
) suggestions would be highly appreciated.

Given that you're using fdopen(), the dup() function is probably
available to you, and I think it could be of use.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jun 27 '08 #2
In article <64**********************************@c65g2000hsa. googlegroups.com>,
Joakim Hove <jo*********@gmail.comwrote:
/* Temporarily close stdout: */
fclose(stdout);
job_nr = lsb_submit( &request , &reply );
/* Reopen stdout */
stdout = fdopen(1 , "a");
I don't think there's any standard C way to to what you want.

It looks as if you're using something Posix-like. The fclose(stdout)
will have closed the underlying file descriptor (1), so your fdopen()
doesn't work. As someone else said, you could use dup() to keep a
copy of it. But standard C doesn't let you assign to stdout; I'm not
sure whether Posix does. I have a vague recollection of some systems
providing fdreopen(), but it doesn't seem to be standard. You could
fdopen() a new FILE *, and use that instead of stdout in your code,
or if you have /dev/fd you could use freopen() on /dev/fd/N, where N
is what you got back from dup().

But closing stdout is dubious in the first place. Are you sure the
library won't complain about stdout being closed? It would be better
to freopen() stdout to /dev/null.

-- Richard
--
:wq
Jun 27 '08 #3
Thank you for answering.

[I have looked at the dup/dup2 calls, I agree that these seem to be
useful for me, but those man-pages are just above my limit I am
afraid.]
It looks as if you're using something Posix-like. *
That is right.
But closing stdout is dubious in the first place. *Are you sure the
library won't complain about stdout being closed?
Yes - I was also uncomfortable with that. I had no strong will to
close() stdout, it was merely a suggestion on my behalf.
It would be better to freopen() stdout to /dev/null.
I have tried:

....
/* Temporarily redirect stdout -/dev/null */
stdout = freopen("/dev/null" , "w" , stdout);

/* Offending call */
lsb_submit( &request , &reply );

/* restore stdout */
stdout = freopen("/dev/stdout" , "w" , stdout);

But - the last freopen() fails with 2:No such file or directory.

Suggestions?

Joakim
Jun 27 '08 #4
In article <42**********************************@e39g2000hsf. googlegroups.com>,
Joakim Hove <jo*********@gmail.comwrote:
/* Temporarily redirect stdout -/dev/null */
stdout = freopen("/dev/null" , "w" , stdout);
Don't assign the result to stdout. freopen() changes stdout itself.
/* restore stdout */
stdout = freopen("/dev/stdout" , "w" , stdout);
>But - the last freopen() fails with 2:No such file or directory.
Probably it closes stdout before trying to open /dev/stdout.

The dup() approach with /dev/fd would be something like (not tested):

char buf[20];
int saved_stdout = dup(1);

freopen("/dev/null", "w", stdout);
lsb_submit( &request , &reply );
sprintf(buf, "/dev/fd/%d", saved_stdout);
freopen(buf, "w", stdout);

To avoid dup(), but still relying on /dev/stdout, do

FILE *mystdout = fopen("/dev/stdout", w);
freopen("/dev/null", "w", stdout);

and change your code to write to write to mystdout instead of stdout.

You'd be wise to ask in a unix, linux or posix newsgroup, because
there may be subtleties to the workings of /dev/stdout and /dev/fd/.

-- Richard
--
:wq
Jun 27 '08 #5
Thanks a lot:
The dup() approach with /dev/fd would be something like (not tested):

* * char buf[20];
* * int saved_stdout = dup(1);

* * freopen("/dev/null", "w", stdout);
* * lsb_submit( &request , &reply );
* * sprintf(buf, "/dev/fd/%d", saved_stdout);
* * freopen(buf, "w", stdout);
Worked like charm :-)

Joakim
Jun 27 '08 #6

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

Similar topics

3
by: Colin McKinnon | last post by:
Hi All, I'm trying to work out how to close the connection to the browser at a defined point within my code, but go on to do some time-consuming processing with PHP. Although it *should* be...
3
by: Alfred von Campe | last post by:
I would think this is simple to do, but I just can't figure it out this morning. Assume I have a function in Perl which writes some output to STDOUT. I want to call this function from a Perl...
4
by: Rouben Rostamian | last post by:
Consider the following demo program: #include <stdio.h> int main(void) { unsigned char c; for (c=0; c<15; c++) putchar(c);
10
by: Michael Gaab | last post by:
If I redirect stdout by using freopen("afile", "w", stdout); and then I closed stdout using, fclose(stdout), essentially I am just closing "afile". I have to reestablish what stdout originally...
16
by: boss_bhat | last post by:
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
1
by: Nico Grubert | last post by:
Dear Python developers, I use a short python script in order to run an external application plus to open a browser displaying a default page. My Setup: Python 2.4.3. / Windows2000 #...
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
7
by: Pietro Cerutti | last post by:
Hi group, I just noticed that a malloc w/out relative free occurs in the standard C library (or at least, on my implementation of it). #include <stdio.h> int main(void) { printf("%s\n",...
3
by: amolbehl | last post by:
Hi, I have too many inserts to deal with but the at specific part of the code is intense on computation where I would like to temporarily close connection and then once the computations are...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
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...

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.