Use process incomplete output in program C

I am using tcpstat in linux environment. I want to write his output in a C program, even if it is not finished yet. I tried using the popen () function, but it can only process the output after . I want to process tcpstat output on the fly when and when it prints it to standard output. How should I do it?

For instance,

$ tcpstat -i wlan0 1
Time:1297790227 n=2 avg=102.50  stddev=42.50    bps=1640.00
Time:1297790228 n=11    avg=86.36   stddev=19.05    bps=7600.00
Time:1297790229 n=32    avg=607.97  stddev=635.89   bps=155640.00
Time:1297790230 n=13    avg=582.92  stddev=585.55   bps=60624.00

The above conclusion continues indefinitely. So I want to handle the output in a C program, and when tcpstat prints something to stdout.

Thanks and respect,

Hrishikesh Murali

+3
source share
2 answers

tcpstat -i wlan0 -a 1 | your_program . , .

popen pipe . - .

+6

tcpstat -F, , . ( stdout)

, popen FILE setbuf, .

setbuf(popen_fd, NULL);

Alternatively, you can configure it to line buffered using setlinebuf

setlinebuf(popen_fd);
+5
source

Source: https://habr.com/ru/post/1792280/


All Articles