LinuxQuestions.org
Review your favorite Linux distribution.
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 09-21-2004, 08:42 PM   #1
wuzhong
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Rep: Reputation: 0
Help!How can I get the file name list in C program?


I mean if I use the function "scandir",how can I get the match file name and list in modified time order asc.

thank you very much!
 
Old 09-21-2004, 09:55 PM   #2
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
you can use ls

system("ls>file")

and then read the file
 
Old 09-21-2004, 10:18 PM   #3
wuzhong
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Maybe this can not run very well when the file number is very large!!
 
Old 09-21-2004, 10:52 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
ls is doing the same work you'd have to do to get the information you want - you would have to write a comparison function for scandir that used stat() to get the modified time of each file, and I think this would turn out slower than just calling ls, due to the number of stat commands. If you're worried about the disk space used by the output, you could use popen instead to read the output directly.
 
Old 09-21-2004, 11:14 PM   #5
cracauer
Member
 
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59

Rep: Reputation: 15
The official way in in C is readdir(3) and friends.

However, a
Code:
  popen("ls foo*.bar", "r");
will do anytime and supports the globbing you might want out of the box. You can also use `find` instead of `ls` for more options and/or recursion into the tree.
 
Old 09-22-2004, 09:36 AM   #6
wuzhong
LQ Newbie
 
Registered: Aug 2004
Posts: 17

Original Poster
Rep: Reputation: 0
Yes,this is a very good way.

and this way is useful for many other resolvent.

thank you!!
 
Old 09-22-2004, 11:56 AM   #7
jdruin
Member
 
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313

Rep: Reputation: 30
Maybe copying the basic code from ls.c would be helpful if you need the functionality but you also need to customize it some. Then you could change the code to work how you need inside your program.
 
  


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
concepts a linked list program in c ssg14j Programming 4 08-19-2005 10:10 AM
How to rm a list of files returned from another program? neo_in_matrix Linux - Newbie 2 04-12-2005 11:24 PM
link list reversl program vijeesh_ep Programming 3 02-27-2005 12:17 AM
Perl program to list listening ports doza Programming 1 07-29-2004 01:57 PM
windows to linux program comparison list ampex189 Linux - Newbie 3 08-19-2003 02:05 AM


All times are GMT -5. The time now is 07:48 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 Google+: linuxquestions
Open Source Consulting | Domain Registration