4

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
EliaTolin
  • 526
  • 3
  • 15
  • `cout <<` very strongly suggests C++, but the C tag hints that the external function is NOT C++. That's an assumption, though. Can you clarify? In pure C++ it's easy. – MSalters Mar 12 '21 at 09:58
  • 9
    That's why a function should do only one thing (computation and display are two things). – Jarod42 Mar 12 '21 at 10:02
  • if by any means you can modify `function` it should at least take the stream as parameter, so you can easily redirect output – 463035818_is_not_an_ai Mar 12 '21 at 10:11
  • @largest_prime_is_463035818 I not written function(). It's external. – EliaTolin Mar 12 '21 at 10:13
  • what does function do? – Marius Bancila Mar 12 '21 at 10:15
  • i understood that. Often we have to make compromises and apply workarounds. In a perfect world `function` would not write to `std::cout`. It does so, but among all workarounds changing `function` would be the best option – 463035818_is_not_an_ai Mar 12 '21 at 10:16
  • 1
    https://stackoverflow.com/questions/13816994/how-to-disable-printf-function seems releated to this one – cagnulein Mar 12 '21 at 10:54
  • Does this answer your question? [Suppress output to cout from linked library](https://stackoverflow.com/questions/3576024/suppress-output-to-cout-from-linked-library) – phuclv Mar 12 '21 at 11:15

3 Answers3

2

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/

ralf htp
  • 9,149
  • 4
  • 22
  • 34
2

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';
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

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.

cagnulein
  • 23
  • 5
  • I interpret OP:s question and comment to ralf's answer as the output from `function()` is wanted, only not right away. This will discard it completely. Also, I'm pretty sure `/dev/stdout` doesn't exist on all platforms. – Ted Lyngmo Mar 12 '21 at 11:13
  • i changed the answer now :) – cagnulein Mar 12 '21 at 11:17