How to return the output string using system("ls *")

Jul 10, 2010 at 8:12pm
Hi,

I am using system("ls *") to list all the files under c++. This function only returns an integer, and I am wondering whether there is a simliar function which can return the output string. Thanks.
Jul 10, 2010 at 8:15pm
What system() does is basically act as though you had typed in that command into the command prompt. It displayed everything just like it would if you had done so. I suppose you could use piping to get the results to a file and read it, but I personally think it would be better just to use your OS's API or another abstraction layer (like boost) to get at the directory information.
Jul 10, 2010 at 8:30pm
Thanks! I am a linux beginner, and could you show me the script for this?
Jul 10, 2010 at 9:04pm
http://www.cplusplus.com/forum/unices/12452/
The example on that link gets a recursive directory listing. You can fix that if you like...

Here is a similar, non-recursive example. It sorts the results, but that can be easily changed, too...
http://www.cplusplus.com/forum/unices/3548/#msg15009

Good luck!
Topic archived. No new replies allowed.