How can I invoke another program or command and trap its output?



How can I invoke another program or command and trap its output?..

Answer / Omendra Kumar Shukla

In C, you can use the `system()` function to execute a command and get its output. Here's an example of invoking the `ls` command and trapping its output:nn```cn#include <stdio.h>n#include <stdlib.h>nnint main() {n char command[] = {"ls"};n int status;n FILE* pipe = popen(command, "r");n char buffer[128] {};n while(!feof(pipe)) {n fgets(buffer, sizeof buffer, pipe);n printf("%s", buffer);n }n pclose(&pipe);n return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

What is output redirection?

1 Answers  


write a c program to find the roots of a quadratic equation ax2 + bx + c = 0

11 Answers   CSC, St Marys, TATA,


I didn't count the ducks that I saw in line, but I do remember that one duck was in front of two ducks, another duck behind two ducks. How many ducks did I see?

2 Answers  


What is this infamous null pointer, anyway?

1 Answers  


program to locate string with in a string with using strstr function

2 Answers   Huawei, Shreyas,


why we are using semicolon at the end of printh statment

2 Answers   HCL,


Write programs for String Reversal & Palindrome check

1 Answers   TISL,


1. What will be the output of the following programs. a) #include <stdio.h> Main() { Int x=4; While(x==1) { X=x-1; Printf(ā€œ%dā€,x); --x; } }

7 Answers   CSC,


4. main() { int c=- -2; printf("c=%d",c); }

1 Answers  


What are the advantage of c language?

1 Answers  


Develop a flow chart and write a c program to find the roots of a quadratic equation ax2+bx+c=0 using switch and break statement.

1 Answers   TCS,


AMMONG THE 4 STROAGE CLASSES IN C, WHICH ONE FASTEST?

13 Answers   HCL,


Categories