7

I'm using someones library that printf's out an error message when a connection to a device is unsuccessful. The library code prints out nothing when a connection is successful.

I periodically check (loop & sleep) to see if a device is connected but I only want to print out when it is connected.

At the moment I get something like:

Waiting for connection... (<-- My print)
Error
Error
Error
Error
Error
Connection successful (<-- My print)

What I want is:

Waiting for connection... (<-- My print)
Connection successful (<-- My print)

How can I programatically ignore a printf?

N.b. I found a similar question Programmatically Ignore Cout but that solution did not work for printf.

I am using Windows.

Can anyone help? (c/c++ novice)

Community
  • 1
  • 1
Kevvvvyp
  • 1,704
  • 2
  • 18
  • 38

4 Answers4

2

Have you tried something like (before establishing connection):

FILE * myout = stdout;
stdout = fopen ("standard-output-file", "w");

To print something to output you could use then:

fprintf(myout, "format", ...);

Edit: Remember to close the file descriptor afterwards:

fclose(myout);
W.F.
  • 13,888
  • 2
  • 34
  • 81
  • I am not wanting to print to a file (if I have understood you correctly), I am wanting to programatically ignore printfs temporarily. – Kevvvvyp May 14 '15 at 10:39
  • 1
    @KevinPaton Yes but you could open e.g. /dev/null as an output if you are in Linux that would be equivalent with ignoring it... – W.F. May 14 '15 at 10:40
  • @KevinPaton on the other hand everything written to `myout` would be shown in standard output as expected... – W.F. May 14 '15 at 10:41
  • It's implementation defined (not everywhere stdout is an assignable FILE*). For example (on Windows) current MSVC++ implementation changed to define stdout as return value of a function (actually function returns an array). – Adriano Repetti May 14 '15 at 11:00
0

I was able to get this working under Linux the following way

#include <cstdio>

using namespace std;

int main() {
   printf("Start\n");
   freopen("/dev/null", "w", stdout);
   printf("middle\n");
   freopen("/dev/stdin", "w", stdout);  // really stdin here!
   printf("End\n");
   return 0;
}

Under Windows I think there are other names for files (nul and con maybe).

However, this seems to be completely unportable, and, what's worse, this will prevent the users from redirecting your program output to a file, because I explicitly reopen /dev/stdin. There are suggestions that you can use /dev/fd/1 instead of /dev/stdin to mitigate the latter problem, but I have not tested it.

It seems that there is no reliable way, see, e.g. http://c-faq.com/stdio/undofreopen.html

Petr
  • 9,812
  • 1
  • 28
  • 52
0

I don't know the absolute solution, but you'll need to use:

freopen("conout$", "w", stderr);

Or something similar, but with "conout$" pseudo-file, which I believe is same as /dev/stdout on Linux.

You may have to use GetStdHandle, AttachConsole etc.

Ajay
  • 18,086
  • 12
  • 59
  • 105
0

If you are using c++11 and above you can use a simple variadic template without much coding so this overloads printf for all cases and the original one never gets called

template <typename ...Args>
void printf(Args... ...)
{
}
tejas
  • 1,795
  • 1
  • 16
  • 34