I'm trying to create a program where it would count the number of directories, or the number of readable/writable/executable files. The user would input only the name of the author and the letters "d", "r", "w", or "x". I tried to directly call "ls -l" in my program but that caused an error. How do you call UNIX commands within a C program?
2 Answers
I tried to directly call "ls -l" in my program but that caused an error. How do you call UNIX commands within a C program?
You can se system in your C program, for example:
system( "ls -l" );
For that to work, you'll also need to #include <stdlib.h>
2 Comments
Dietrich Epp
I think the program output is necessary in this case too.
Stack Exchange Broke The Law
In that case use
popen
system()system()'works', you'd have to manufacture the command line carefully; there's no way for the launching program to filter the output (in the ordinary course of events). You might need to use POSIX functionspopen(), or maybefork()andexecvp()and related functions (pipe(),dup2(), …).nftw(),opendir(),readdir(),stat().