Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4104908
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:10:41+00:00 2026-05-20T21:10:41+00:00

I actually have a solution to this problem, but I’m wondering if there is

  • 0

I actually have a solution to this problem, but I’m wondering if there is a slicker one.

I have the need to load in a library to my utility using dlopen and then call one of the functions.

Unfortunately, the function spews a whole bunch of information out onto STDOUT and this I do not want.

I have a solution that is non-portable and I’m wondering if there’s a better, more generic solution that I could use.

Here’s what I have (NB: This is C) :

/*
 * Structure for retaining information about a stream, sufficient to
 * recreate that stream later on
 */
struct stream_info {
    int fd;
    fpos_t pos;
};
#define STDOUT_INFO 0
#define STDERR_INFO 1

struct stream_info s_info[2];
point_stream_to_null(stdout, &s_info[STDOUT_INFO]);
point_stream_to_null(stderr, &s_info[STDERR_INFO]);

void *output = noisy_function();

reset_stream(stderr, &s_info[STDERR_INFO]);
reset_stream(stdout, &s_info[STDOUT_INFO]);

/*
 * Redirects a stream to null and retains sufficient information to restore the stream to its original location
 *** NB ***
 * Not Portable
 */
void point_stream_to_null(FILE *stream, struct stream_info *info) {
    fflush(stream);
    fgetpos(stream, &(info->pos));
    info->fd = dup(fileno(stream));
    freopen("/dev/null", "w", stream);
}

/*
 * Resets a stream to its original location using the info provided
 */
void reset_stream(FILE *stream, struct stream_info *info) {
    fflush(stream);
    dup2(info->fd, fileno(stream));
    close(info->fd);
    clearerr(stream);
    fsetpos(stream, &(info->pos));
}

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:10:41+00:00Added an answer on May 20, 2026 at 9:10 pm

    I have a suggestion, which lets you use the preprocessor for portability, or perhaps “portability”.

    If you try something like

    #if defined __unix__
    #define DEVNULL "/dev/null"
    #elif defined _WIN32
    #define DEVNULL "nul"
    #endif
    

    (ignoring other OSes, else case, error directive, etc.)
    and then reopen the file as before

    FILE *myfile = freopen(DEVNULL, "w", stream);
    

    then that may give you what you want.

    I haven’t tried this at home, though. The “nul” file exists; see /dev/null in Windows. And you can get predefined macros at “Pre-defined C/C++ Compiler Macros”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I actually have two questions regarding the same problem but I think it is
I actually have an answer to my question but it is not parallelized so
I would actually love to have an AlternatingItemTemplate on a GridView, but all it
I always have found the open source space interesting but have never actually participated
I have heard that it's best not to actually have any html in your
I have a JTree with multiple roots (Of course, I actually have an invisible
I used windows installer (msi project) and actually I have the msi file after
I have a JLabel (actually, it is a JXLabel). I have put an icon
I have a line (actually a cube) going from (x1,y1,z1) to (x2,y2,z2). I would
I have a JPEG image (actually a BLOB in a database) which I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.