3

Is there a way to run the linux command ls, from c++, and get all the outputs stored in one array, in c++?

Thanks

user1584421
  • 3,499
  • 11
  • 46
  • 86
  • Duplicate of http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c/479103 ? – alestanis Oct 20 '12 at 09:53

1 Answers1

5

If you insist on actually running ls, you can use popen to launch the process and read the output:

FILE *proc = popen("/bin/ls -al","r");
char buf[1024];
while ( !feof(proc) && fgets(buf,sizeof(buf),proc) )
{
    printf("Line read: %s",buf);
}

But you could probably better be reading the directory contents and file info yourself, using opendir and readdir.

mvds
  • 45,755
  • 8
  • 102
  • 111