473,992 Members | 3,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disabling printf, enabling something else

Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are hidden.
No macros or replacements please. I'm looking for good looking in one place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );

Dz
Apr 1 '08 #1
3 1898
On 2008-04-01 22:35, D¿ejm wrote:
Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are hidden.
No macros or replacements please. I'm looking for good looking in one place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );
Wrap the printfs in a function which contains some if-statement checking
for whatever condition you want. Or use fprintf and put the special
output in cerr/clog.

--
Erik Wikström
Apr 1 '08 #2
D¿ejm wrote:
Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are hidden.
No macros or replacements please. I'm looking for good looking in one place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );

Dz

Some platforms allow you to do:
freopen("filena me", "w", stdout);

Probably not portable. Probably not even advisable (though my man page
suggests using freopen for exactly that).

--
Alan Johnson
Apr 2 '08 #3
Alan Johnson wrote:
D¿ejm wrote:
>Hello,

We have a code with lot of printfs. Now I would like to introduce special
mode, in which only special messages are displayed and all other are
hidden.
No macros or replacements please. I'm looking for good looking in one
place
solution :).

Disabling printf is easy:
fclose( stdout );
But then I can't output anything.

I thought about duplicating handle, and opening my own output. But
then all
the printfs are visible again somehow:

int fd = dup( _fileno( stdout ) );
fclose( stdout );
printf( "Should NOT be visible. It is not. Good.\n" );
FILE * specialOutput = _fdopen( fd, "w" );
fprintf( specialOutput, "Should be visible. It is. Good.\n" );
printf( "Should NOT be visible, but it is. Bad.\n" );

Any other ideas how to do this ?

PS. There are also cout used. But this probably will do the trick:
cout.clear( ios::badbit );

Dz


Some platforms allow you to do:
freopen("filena me", "w", stdout);

Probably not portable. Probably not even advisable (though my man page
suggests using freopen for exactly that).
Which, upon rereading, appears to not achieve your goal at all. I
should stop posting late at night.

--
Alan Johnson
Apr 2 '08 #4

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

Similar topics

5
6281
by: sonic_soul | last post by:
Hello, I am finishing up creating a fairly complex page that is very rich in DHTML. In addition to updating it self every couple of seconds, various components on it support sync and async communication with various web services. The problem is that if a user tries to click on any JS driven content before the page is fully loaded, it will not work properly since it relies on many onLoad scripts that initiate webservices etc.
5
1825
by: Cillies | last post by:
Hi All, This message is a continuation of an earlier post, so please accept my apologies as I believe I would get a better response this way. Problem: I have 4 combo boxes which I want to able to enable/ disable depending on the selection made in the first combo.
7
96371
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
2
2849
by: HumptyDumpty | last post by:
Does anyone know if there is a problem with re-enabling the Screen Saver after it has been disabled programmatically. I am using the SystemParametersInfo function within User32.dll, and have been successful in disabling the Screen Saver, but it does not work for re-enabling. The only way I can re-activate it is to re-apply the settings through the Display properties in the Control Panel. Sample lines of code, in simplest form:
0
303
by: VSK | last post by:
Hi all, In our ASP.NET web application we have to enable or disable features in each ASP.NET page based on role assigned to user. Ex: if user who logs in is superisor then he can change phonenumber in page1.aspx if user who logs in is finaceofficial then he can just view the phone number in page1.aspx
4
2899
by: Rich P | last post by:
Greetings, I have a routine I was running in VB6 on a timed schedule. When the timeframe came up, the timer would be disable, the routine would run, and the timer gets enabled. I am trying to migrate this project to vb.net. But when the timer gets disable it is not getting re-enabled after the routine is completed. I added Application.DoEvents just before the call to Timer1.Enabled = True. But that did not help. Is there something...
3
4774
by: Giannis Papadopoulos | last post by:
I want to create a macro that it must be ignored when NDEBUG is defined. The macro I've created is #ifndef NDEBUG # define ERROR_MESSAGE(s) fprintf(stderr, "%s(): %s\n", __func__, (s)); #else # define ERROR_MESSAGE(s) #endif
2
6336
by: dougawells | last post by:
Hi- I'm wanting to have a set of radio buttons disabled when a form is displayed, then if they check another specific radio button, those would become enabled. I've tried setting it via window.document.formname.radiogroup.disabled="true"; (or false) - but that doesn't seem to work. I've seen this done with text boxes, but not with radio buttons. Can anyone give any help? Thanks
1
5254
by: hello2008 | last post by:
Hi, I have just started coding in PHP. I have coded a web page using HTML, JS, and PHP. An HTML table has to be populated dynamically using the data from the backend. Presently I have 5 records in the backend table so I get 5 HTML-table rows. I have created a session object starting from the login page. The requirement is that as soon as I log in and my request gets forwarded to the foll PHP page I should be seeing the foll. dynamically...
0
10228
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
11924
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
11493
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...
0
10996
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
8552
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
6505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6665
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4829
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3842
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.