LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 11-24-2009, 06:49 AM   #1
weeshalll
LQ Newbie
 
Registered: Nov 2009
Posts: 11

Rep: Reputation: 0
Reading all files in a directory in an alphabetical order


Hi all,
I want to read all the files in a directory alphabetically .
How can i achieve it ?
Can i use the "readdir" function ?
 
Old 11-24-2009, 06:59 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Which programming languages are you considering?
 
Old 11-24-2009, 07:07 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You can use readdir, sure, but you would still need to code the sorting yourself.

scandir() is more convenient when you need to sort. At the end of man 3 scandir there's example code which is a ready-made program to read a directory sorted alphabetically.
 
Old 11-24-2009, 07:37 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
for a quick lazy solution
you could cheat and use popen


Code:
#include <stdio.h>
#define SZ 8192
int main (int argc, char ** argv) {

    FILE *fp = popen("ls", "r");
    char buffer[SZ+1];
    while (fgets(buffer, SZ, fp)) {
        puts(buffer);
    }

    return 0;
}
 
0 members found this post helpful.
Old 11-24-2009, 11:05 PM   #5
weeshalll
LQ Newbie
 
Registered: Nov 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for your replies

@catkin
i am using C
 
Old 11-25-2009, 12:35 AM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by bigearsbilly View Post
for a quick lazy solution
you could cheat and use popen


Code:
#include <stdio.h>
#define SZ 8192
int main (int argc, char ** argv) {

    FILE *fp = popen("ls", "r");
    char buffer[SZ+1];
    while (fgets(buffer, SZ, fp)) {
        puts(buffer);
    }

    return 0;
}
readdir is far more portable then popen.
 
Old 11-25-2009, 03:19 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by smeezekitty View Post
readdir is far more portable then popen.
all POSIX compliant OSes have it

that's portable enough for me
 
Old 11-25-2009, 10:28 PM   #8
weeshalll
LQ Newbie
 
Registered: Nov 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you all for the replies ...
I checked both the solutions and they are working fine for me
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Now LQ has so many distributions how about listing them in alphabetical order? catkin LQ Suggestions & Feedback 6 07-01-2009 09:07 AM
Kmenu: alphabetical order? sloteel Linux - Software 3 06-10-2008 04:17 PM
How-to list contents of text file in alphabetical order? sucram2g Linux - Software 1 02-19-2007 03:39 AM
Arranging Files In Alphabetical order swatward Linux - General 4 12-11-2006 08:14 PM
how to 'sort' by file extension then alphabetical order adamrosspayne Linux - Newbie 4 07-04-2006 10:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration