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

  • SEARCH
  • Home
  • 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 924143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:18:00+00:00 2026-05-15T19:18:00+00:00

I read about freopen to redirect all printf to a file, but I would

  • 0

I read about freopen to redirect all printf to a file, but I would like the output to be printed on the screen as well. Is there an easy way to redirect the printfs to a file and get the cmd line output?

Thanks!

  • 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-15T19:18:01+00:00Added an answer on May 15, 2026 at 7:18 pm

    From outside the program, use “tee”:

    # echo foo | tee foo.txt
    foo
    # cat foo.txt
    foo
    

    In fact, you could popen() a channel to a tee that writes the file, though that’s system-heavy. Something to this effect:

    FILE *stream_to_write_to = popen( "tee filename.txt" );
    fprintf( stream_to_write_to, "goes to filename.txt and stdout\n" );
    

    I’m curious to see if there’s a from-C quick way of doing this, because at some level, this involves copying data. It’s easy to get two filehandles to write to the same place, use dup() or the like, but the opposite is more tricky. It might involve pushing a module (the common example is “connld” onto a stream), though honestly I’ve never seen this used, so I’d love to see a working code sample myself.

    Best reference I can give is “Advanced Programming in the UNIX Environment” by Stevens.

    Update:

    To speak to R’s comment below, the above solution is a slightly heavier version of just fork/exec-ing and redirecting the child’s handle someplace else. Both will solve the problem, though I prefer the above because it’s easier to clean up, but honestly, both solutions are pretty heavy. Fork() is no lightweight function. If the spirit of the question is to do so without fork/exec, then I’m not sure, I’d love to know, too. If fork/exec is okay, then directly using it or using popen() will hack it.

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

Sidebar

Related Questions

Ive read about it and to be honest it all seems like a bunch
I like to read about new and clever algorithms. And I like to think
I read about a string/word that did so the Routing crashed but I can't
I read about virtual constructors are used for implementing some design patterns, but didn't
I read about the Conditional attribute today. According to MSDN: Applying ConditionalAttribute to a
I read about using the CASE expression inside the WHERE clause here: http://scottelkin.com/sql/using-a-case-statement-in-a-sql-where-clause/ I'm
I have read about partial methods in the latest C# language specification , so
From what I've read about Windsor/Microkernel it is in theory possible to do everything
I've read about and dabbled with some including active record, repository, data transfer objects.
Everything I read about better PHP coding practices keeps saying don't use require_once because

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.