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

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 1863
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("filename", "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("filename", "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
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...
5
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...
7
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 %%...
2
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...
0
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...
4
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...
3
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...
2
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...
1
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.