Reopen a stream with a different file and mode: how to use freopen
#include <stdio.h> int main () { freopen ("my.txt","w",stdout); printf ("This line is redirected to a file."); fclose (stdout); return 0; }