linux - Copy stdout contents to a file in command line mode -
i have program this. in terminal mode. want copy outptu contents file. first tried redirecting, didnt work due buffering. tried unbuffer command. didnt work correctly cases below
file
main() { int i; printf("starting\n"); scanf("%d",&i); printf("the value %d\n",i); }
output
# ./a.out starting 4 value 4
output unbuffer command
# unbuffer ./a.out | tee tt starting 4 ^c
output simple redirection [works order of output not correct]
# ./a.out | tee tt 5 starting value 5
i want contents shown in screen directly copied file. working in terminal mode [no gui].
unbuffer
doesn't read standard input @ default, program waits without ever getting input. can make read , pass on standard input -p
option:
unbuffer -p ./a.out | tee tt
will work. downside doesn't display type write.
alternatively, if control c program, can disable default buffering of standard output when it's not terminal. can use, e.g., setbuf:
setbuf(stdout, null);
or manually flush after each output.
Comments
Post a Comment