ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
How can I read a listing of a directory's content (into an array of chars for example)?
The only way I can think of is to call system("ls>contents") and then read from the file created.
how about a little thing called "context" ? what language are you using? what are you doing with this data? please try to provide more information in your questions.
,----[ man (3) ftw ]
| ftw() walks through the directory tree starting from the
| indicated directory. For each found entry in the tree,
| it calls funcptr with the full pathname of the entry
| relative to directory, a pointer to a the second
| argument is a pointer to the stat (2) structure for the
| entry and an int, which value will be one of the
| following:
`--
I thought about this a little more and came to the conclusion that perhaps the ftw() routine is overkill. In view of your original remark
the only way I can think of is to call system("ls>contents") and then read from the file created.
maybe the popen() system call is for you. That way, you won't have to deal with a file on the disk, you can simply write the appropriate command --possibly a pipeline such as
ls -t | head -10
for instance-- and then read the output from a standard FILE * stream.
In C, you want to use either the opendir/readdir/closedir combo, or also try scandir. scandir takes care of all the mess of using opendir/readdir/closedir, as well as memory allocation for the dir tree, but you'll have to remember to free up that memory when done. man scandir has an example available of how it is used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.