5

I am outputting to stdout. How can I redirect that to a new file through code? While we run the program we can redirect like ./sample > test.txt. How can I do this when executing the sample program itself ? (C programming)

3 Answers 3

8

You probably want to use freopen.

Example from reference:

#include <stdio.h>
...
FILE *fp;
...
fp = freopen ("/tmp/logfile", "a+", stdout);
Sign up to request clarification or add additional context in comments.

Comments

3

Use freopen().

Comments

0

Use dup2() system call and redirect the output to a file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.