LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-11-2002, 05:59 PM   #1
vank
LQ Newbie
 
Registered: Oct 2002
Posts: 5

Rep: Reputation: 0
Get directory contents


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.

[edit]
I'm using C
[Edit]

Last edited by vank; 10-11-2002 at 06:35 PM.
 
Old 10-11-2002, 06:03 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
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.
 
Old 10-12-2002, 10:41 PM   #3
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 16
this sounds like a job for ftw()

,----[ 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:
`--
 
Old 10-13-2002, 12:23 PM   #4
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 16
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.

To get all the good stuff, man popen

good luck.

Last edited by leed_25; 10-13-2002 at 12:25 PM.
 
Old 10-14-2002, 08:58 AM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
Old 10-16-2002, 01:14 PM   #6
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,755

Rep: Reputation: 88
I posted a program long ago that will read the contents of a directory.

http://www.linuxquestions.org/questi...threadid=21766
 
Old 10-19-2002, 10:13 AM   #7
vank
LQ Newbie
 
Registered: Oct 2002
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks (all of you)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot list directory contents nocturna_gr Linux - General 2 11-29-2005 05:24 PM
Empty directory contents stefaandk Linux - General 1 08-22-2005 07:03 AM
Installing contents of second cd through premounted directory pele_smk Slackware 3 12-16-2003 12:22 PM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 07:03 PM
List the contents of a directory to a file MasterC Linux - General 19 11-26-2002 10:09 AM


All times are GMT -5. The time now is 04:38 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration