c - fprintf both to file and to stdout -
this question has answer here:
- how print both stdout , file in c 4 answers
my c program expected produce lot of output.
some of these output lines special , write copy special file. end doing
file * outf = fopen("special_file", "a"); fprintf(stdout, "normal line 1\n"); fprintf(stdout, "special line!\n"); fprintf(outf, "special line!\n");/*inelegant & dangerous code duplication*/ fprintf(stdout, "normal line 2\n");
is there easy way avoid such inelegant & dangerous code duplication? have quite lot of , may write things printf("next %d\n", num++);
cannot duplicated naively, e.g. using macros.
at moment see no solution short of spawning child process run tee
equivalent. other idea? e.g. can 1 define kind of file*
in way redirect both stdout
& outf
?
i think dup(2) system call need, see "man 2 dup".
edit: votes, think got wrong.
what dup make 2 file descriptors reference 1 file. want single file descriptor-like object references 2 files. that's quite different thing.
Comments
Post a Comment