1

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;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • No — at the shell level, you can have the program write to stdout only (not opening `out.txt` at all) and then run `./yourprog | tee out.txt`, but otherwise you'll have to arrange to open `out.txt` on a file stream (for example, `FILE *ofp = fopen("out.txt", "w");`, and check that the open succeeded), and then write to both `stdout` and `ofp`. – Jonathan Leffler Oct 02 '20 at 13:20
  • @JonathanLeffler there's also a system call `tee` (which is used to implement `tee` utility). – domen Oct 02 '20 at 13:23
  • @domen — only on Linux, not in standard C. The question is not tagged with Linux, and the code contains no other Linux-only construct. Also the man page for [`tee()`](http://man7.org/linux/man-pages/man2/tee.2.html) talks about the file descriptors needing to be pipes specifically — they are not pipes in this program. On the whole, `tee(2)` is irrelevant — especially as it is a file descriptor function, not a file stream function. – Jonathan Leffler Oct 02 '20 at 13:24
  • @JonathanLeffler but it's fair play for you to advise unix shells and utilities? :P – domen Oct 02 '20 at 13:27
  • @dome: It's perfectly fair play to suggest appropriate alternatives — as long as you include any caveats about why the alternatives may not be appropriate everywhere (if there are limitations on their use). For the multiple reasons outlined in my comment (limited to Linux; limited to pipes; using file descriptors, not file streams), the `tee(2)` system call is so limited that it really isn't much help. (I'd argue it isn't much help in general; it is a bit weird that such a restricted system call was included in Linux at all — Ritchie and Thompson would not have added it to 7th Edition Unix.) – Jonathan Leffler Oct 02 '20 at 13:31
  • I guess I'll just have to use fprintf. I thought there would be a simple solution to output back to the terminal screen after using freopen. – Keshav Ujoodha Oct 02 '20 at 13:46
  • @JonathanLeffler That's an interesting position on `tee`. I don't think it's is restricted. `tee` and `splice` are incredibly useful. – William Pursell Oct 02 '20 at 16:01
  • @WilliamPursell: https://man7.org/ on `tee(2)` says: _`tee()` duplicates up to `len` bytes of data from the pipe referred to by the file descriptor `fd_in` to the pipe referred to by the file descriptor `fd_out`. It does not consume the data that is duplicated from `fd_in`; therefore, that data can be copied by a subsequent `splice(2)`._ Is that description inaccurate? I'll take your word for it that you can find uses for it; it is not a facility I think I've ever had a burning need for. In some very specialized cases, it might be useful, but not often given the requirement for two pipes. – Jonathan Leffler Oct 02 '20 at 16:46
  • @JonathanLeffler It allows you to push a lot of IO out of user space. Sure, it's a specialized use case (so, restricted in that sense), but can be a huge performance benefit. – William Pursell Oct 02 '20 at 16:51
  • @WilliamPursell: Yes … but only if you're reading from one pipe and writing to another without transforming the data. In general, a program can't tell whether a given file descriptor is a pipe — the information isn't explicitly available from a call to `fstat()`. However, there's no great purpose in continuing the discussion. It isn't an appropriate function for the code in the question. I stand by my opinion about `tee`; you stand by yours. We have different backgrounds to colour what is useful to us. (I don't work exclusively on Linux; that makes a Linux-only system call unattractive to me.) – Jonathan Leffler Oct 02 '20 at 16:56

0 Answers0