Is The benefit of using popen is only to read the ouput produced by a command or there are some more benefits or advantages of popen over system.
Consider two programs below:
Program 1:
#include <stdio.h>
int main()
{
FILE *more,*who;
if (!(more = popen("more", "w")))
{
printf("Command `more` not found!");
return -1;
}
if (!(who = popen("who", "r")))
{
printf("Command `who` not found!");
return -1;
}
while (!feof(who))
{
char buffer[100];
if (fgets(buffer, 100, who) != NULL)
{
fputs(buffer, more);
}
}
fclose(more);
fclose(who);
return 0;
}
Program 2:
#include <unistd.h>
int main()
{
system("who|more");
return 0;
}
Why should i use Program1 if i can do the same thing in one line as done in Program2.