Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Ignore printf

Tags:

c++

printf

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)

like image 535
Kevvvvyp Avatar asked May 14 '15 10:05

Kevvvvyp


1 Answers

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);
like image 79
W.F. Avatar answered Oct 10 '22 09:10

W.F.