SuperSpider
SuperSpider

Reputation: 17

C - UNIX commands in program

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?

Upvotes: 0

Views: 339

Answers (2)

Scott Sosna
Scott Sosna

Reputation: 1413

Using the find command might work better, you could count directories with the command "find . -t d | wc -l" and do something similar for files with the appropriate flags.

Upvotes: 0

tdao
tdao

Reputation: 17713

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>

Upvotes: 1

Related Questions