0

I am doing my assignment and would like to store all sub directories in the proc directory. One option is to use a file to store all and later store it back to a string like this.

 system("ls -1 /proc | sort > hw4data.txt "); 

    myFile.open("hw4data.txt");     
    myFile >> fdata;

But I am not allowed to use an extra file. But I want the list of all subdirectories in the /proc directory.

sad
  • 820
  • 1
  • 9
  • 16
  • Do you want to `store` or to `sort`? – Cristik Apr 29 '15 at 23:42
  • I want to store the sorted list of all sub directories. Thanks though! – sad Apr 29 '15 at 23:42
  • So if you don't want to use an extra file, what do you want to use? – Cristik Apr 29 '15 at 23:45
  • like an vector of strings where each entry is the name of the subdirectory. – sad Apr 29 '15 at 23:46
  • You might want to look up (for one example) Boost [directory_iterator](http://www.boost.org/doc/libs/1_58_0/libs/filesystem/doc/reference.html#Class-directory_iterator). – Jerry Coffin Apr 29 '15 at 23:52
  • You can use `popen()` to execute a shell command and read the output from it. Then you can read each line into a string, which you put into an array. – Barmar Apr 29 '15 at 23:56
  • See also http://stackoverflow.com/questions/3190514/popen-equivalent-in-c for C++ equivalents to `popen()`. – Barmar Apr 29 '15 at 23:56
  • @Barmar, I have already a pipe set up between the child and the parent. The parent should send each subdirectory name to the child. How can I again use the popen(),? Thanks though! – sad Apr 30 '15 at 00:55
  • The child can substitute the directory name into the `ls` command, and then call `popen()` with that command to read the output. – Barmar Apr 30 '15 at 00:57

0 Answers0