I'm trying to print my program to console as well as an output file in my c program.
I'm using FILE *outfile = freopen("out.txt","w",stdout);
to make the console output redirect to the output file. But then the redirection prevent printf
to output to the terminal. It just compiles and fills out out.txt
Is there a way to make it print to terminal as well as out.txt
without using fprintf
or piping to the terminal using >
?
int main() {
FILE *infile = fopen("in.txt", "r");
FILE *outfile = freopen("out.txt","w",stdout);
if (infile == NULL) {
printf("Cannot open file \n");
exit(1);
}
readFunction(infile);
fclose(infile);
fclose(outfile);
return 0;
}