Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get output of ls command to an array in c++

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

Thanks

like image 476
user1584421 Avatar asked Dec 03 '25 17:12

user1584421


1 Answers

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.

like image 93
mvds Avatar answered Dec 06 '25 08:12

mvds