473,616 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8966
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************ *************** *******@c65g200 0hsa.googlegrou ps.com>,
Joakim Hove <jo*********@gm ail.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************ *************** *******@e39g200 0hsf.googlegrou ps.com>,
Joakim Hove <jo*********@gm ail.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
6936
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 possible to do this by moving the slow stuff into a function registered with register_shutdown_function, it appears that in many cases, the connection is not closed before calling the shutdown function - there are also issues with calling other...
3
5505
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 script and capture its output in a variable. If this was an executable or even another Perl script, I could use the open() function. But since this is a Perl function, I can't use open(). Or can I? There's got be be a way, but my brain is just...
4
25651
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
21129
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 pointed to? thanks
16
39486
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
7441
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 # ---------------------------------------------------------------------- # Script "shortcut.py" import os
20
7279
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 stream so that the tape driver will write a filemark on the tape. Otherwise multiple clumps of data saved to tape would all appear to be one big file on the tape. When the tape unit device was explicitly opened with fopen() that's possible:...
7
6591
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", "Hello"); return (0); }
3
1473
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 done I could restart it. I dont care if time gets wasted. Sub Main() ' First we initialize paths for all needed files gstrDBPath = "c:\dvp\PKB"
0
8146
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
8647
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...
0
8592
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 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...
1
8297
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
8449
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...
1
6097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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();...
1
2579
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
0
1445
bsmnconsultancy
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...

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.