EliaTolin
EliaTolin

Reputation: 638

It's possible suppress output about of one function?

In my program I use an external function which generates output to me and I don't want it not to be generated by that function alone, it's possible?

int main()
{
    int a; 
    //I don't want the output of this function
    a = function();
    
    //now i want output
    cout << "the result is : " << a; 
}

Is it possible?

EDIT:

The function is in an external library.

Upvotes: 4

Views: 732

Answers (3)

cagnulein
cagnulein

Reputation: 23

use

fclose(stdout);

with this function you will suppress any printf inside your libraries. Of course you can not print other stuff inside your software.

Upvotes: 1

Ted Lyngmo
Ted Lyngmo

Reputation: 117298

Using only standard C++ where no dup-like functions exist, you could open a temporary std::FILE and std::swap with stdout.

#include <cerrno>
#include <cstring>
#include <cstdio>

#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>

// extern "C" int function(); // an assumption

// A helper class to temporarilly redirect the output to stdout to a file and to read
// the content of the file afterwards.

class BufferStdout {
public:
    // the collector string is used for collecting the output to stdout
    BufferStdout (std::string& collector) :
        m_collector(collector),
        fp(std::fopen("output.txt", "w"))
    {
        if(fp == nullptr) throw std::runtime_error(std::strerror(errno));
        std::swap(stdout, fp); // swap stdout and the temp file
    }

    ~BufferStdout () {
        std::swap(stdout, fp); // swap back
        std::fclose(fp);

        // read the content of the temp file into m_collector
        if(std::ifstream is("output.txt"); is) {
            m_collector.append(std::istreambuf_iterator<char>(is),
                               std::istreambuf_iterator<char>{});
        }
        std::remove("output.txt"); // cleanup
    }

private:
    std::string& m_collector;
    std::FILE* fp;
};

int main()  {
    std::string collector; // the string that will contain the output from function()
    int a;
    {
        BufferStdout foo(collector);
        a = function();
    }
    std::cout << "the result is : " << a << '\n';
    std::cout << "Collected from function():\n";
    std::cout << collector << '\n';
}

Upvotes: 2

ralf htp
ralf htp

Reputation: 9422

Yes it is generally possible but a bit complicated, a similar question is in Suppress output to cout from linked library

In addition to you can redirect stdout before invoking the shared library function and then redirect it again after the use of the shared library function in the however this is also a suboptimal solution. Best solution would be to adapt the shared library

// Cpp program to redirect cout to a file 
#include <fstream> 
#include <iostream> 
#include <string> 



using namespace std; 
  
int main() 
{ 
    fstream file; 
    file.open("cout.txt", ios::out); 
    string line; 
  
    // Backup streambuffers of  cout 
    streambuf* stream_buffer_cout = cout.rdbuf(); 
    streambuf* stream_buffer_cin = cin.rdbuf(); 
  
    // Get the streambuffer of the file 
    streambuf* stream_buffer_file = file.rdbuf(); 
  
    // Redirect cout to file 
    cout.rdbuf(stream_buffer_file); 
  
    cout << "This line written to file" << endl; 
  
    // Redirect cout back to screen 
    cout.rdbuf(stream_buffer_cout); 
    cout << "This line is written to screen" << endl; 
  
    file.close(); 
    return 0; 
} 

Note: The above steps can be condensed into a single step

auto cout_buf = cout.rdbuf(file.rdbuf())

// sets couts streambuffer and returns the old 
streambuffer back to cout_buf

source : https://www.geeksforgeeks.org/io-redirection-c/

Upvotes: 2

Related Questions