Write simultaneousely to two streams

前端 未结 5 2012
-上瘾入骨i
-上瘾入骨i 2020-12-11 19:06

Is there a way to to couple two streams (or file descriptors) together so that writing to one stream will also write to the second one? (C, Linux)

Thanks.

5条回答
  •  一整个雨季
    2020-12-11 19:38

    The first thing that came to mind to me was also "tee". So, let's combine C and the shell with popen:

    FILE * multi_out;
    
    multi_out = popen( "tee file1.out > file2.out", "w");
    /* error checks, actual work here */
    pclose( multi_out);
    /* error checks here */
    

    As a Unix bigot, I have assumed you are not trying this on Windows.

提交回复
热议问题