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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-21-2004, 08:42 PM
|
#1
|
|
LQ Newbie
Registered: Aug 2004
Posts: 17
Rep:
|
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!
|
|
|
|
09-21-2004, 09:55 PM
|
#2
|
|
Member
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415
Rep:
|
you can use ls
system("ls>file")
and then read the file
|
|
|
|
09-21-2004, 10:18 PM
|
#3
|
|
LQ Newbie
Registered: Aug 2004
Posts: 17
Original Poster
Rep:
|
Maybe this can not run very well when the file number is very large!!
|
|
|
|
09-21-2004, 10:52 PM
|
#4
|
|
Member
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900
Rep:
|
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.
|
|
|
|
09-21-2004, 11:14 PM
|
#5
|
|
Member
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59
Rep:
|
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.
|
|
|
|
09-22-2004, 09:36 AM
|
#6
|
|
LQ Newbie
Registered: Aug 2004
Posts: 17
Original Poster
Rep:
|
Yes,this is a very good way.
and this way is useful for many other resolvent.
thank you!!
|
|
|
|
09-22-2004, 11:56 AM
|
#7
|
|
Member
Registered: Jul 2003
Location: Louisville aka Derby City
Distribution: WinXP SP2 and SP3, W2K Server, Ubuntu
Posts: 313
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:13 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|