diren.h : readdir( ) - sorting requirement


 
Thread Tools Search this Thread
Top Forums Programming diren.h : readdir( ) - sorting requirement
# 1  
Old 06-03-2008
Question diren.h : readdir( ) - sorting requirement

hi all,
im using dirent.h headers readdir() function to traverse down a directory ( after openning it with opendir() ), and im printing the dir->d_name in a program.
Code:
while(( dir = readdir( d ) ) != NULL ){
	
   if (stat([path+dir->d_name], &info) != 0){ 
      fprintf(stderr, "stat() error on %s: %s\n", path, strerror(errno));
      continue;
      }

if (S_ISDIR(info.st_mode) || S_ISREG(info.st_mode)) { 
   printf("%s\n", dir->d_name);
}
else{continue;}

}

note:[path+dir->d_name] is the path and current dir entrys name.

but the output this gives is not sorted according to any parameter(name, date.etc..).
is there a way to get such a sorted output using above dirent.h function calls or any other.
any help is greatly appreciated.
wolwy.
# 2  
Old 06-03-2008
Check out scandir(3) and alphasort().
# 3  
Old 06-08-2008
Thankx fpmurphy for the reply, actually i had it given up until i saw ur answer recently. it worked, up to my requirement, thankx.
wolwy.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX - requirement

Hi All, I have a source file with data Name ~ Groups Muni~abc,was,USA_ax,123 Chaitanya~USA_12,was Balaji~123,xyz,was Ramu~123,xyz From the second column i want to extract only the groups that matches the pattern 'USA_%' or if the group = 'was', and ignore any other columns. ... (8 Replies)
Discussion started by: morbid_angel
8 Replies

2. Shell Programming and Scripting

Looping requirement

Hi all, I have little working knowledge in unix shell scripting. I have a requirement where i need to pull out some data in between the strings in the file. Input: TEST a a c f d TEST f e g g TEST Output: (7 Replies)
Discussion started by: satyasrin82
7 Replies

3. Programming

readdir and dynamic array memory corruption

Hi everyone I am developing an utility. At some part of it I read directory entries to a dynamic array: struct list It stores pointers to items: list.entries, which are structures: struct entry If a number of files in a directory is greater then number of elements an array was initially... (11 Replies)
Discussion started by: torbium
11 Replies

4. SuSE

problem of readdir on IA64 suse

Dear Experts, i am trying to find whether the given name is file or a directory dirp = opendir(dirname); direntp = readdir(dirp); if(direntp->d_type & DT_DIR) { printf("\n is a dirctory"); } else { //dir_or_file = Mtrue; printf("\n not a directory"); } it always... (9 Replies)
Discussion started by: vin_pll
9 Replies

5. Shell Programming and Scripting

grep readdir

Quick question. I can not get the context corrert on this code. opendir(DIR, "."); @fileldiv = grep(/l*/,readdir(DIR)); closedir(DIR); I am trying to search all html files within a dir that start with l. Thanks for your help. (1 Reply)
Discussion started by: mrlayance
1 Replies

6. Shell Programming and Scripting

Requirement

I am trying to script and came up with a conclusion that I need a do while loop in my statement. I am stuck with the do while syntax. I need to use it alongwith the if then else statement. Can I use it is a big question? I actually need to get all the files that are there from within run_dt to... (1 Reply)
Discussion started by: aronmelon
1 Replies

7. Programming

Finding wildcards in readdir

I am having a hard time doing this and can't seem to find an example to help me. This is my code: DIR *dirp=opendir(pathname); struct stat filebuf; struct dirent entry; struct dirent *dp=&entry; RWCString pattern; for (int i = 0; i < request_->getNumStreams(); i++) { ... (2 Replies)
Discussion started by: ajgwin
2 Replies

8. UNIX for Dummies Questions & Answers

how to man readdir(3)

I read the description of the command readdir by using 'man readdir'. However, in the description i was suggesed to refer to readdir(3). I wonder how to see the manual of readdir(3) Thanks (1 Reply)
Discussion started by: cy163
1 Replies

9. Filesystems, Disks and Memory

Preblem with readdir system call

I am trying to read the directory contents throgh the readdir system call. After getting the directory entry I am testing the type of it by using the folllowing macros S_ISDIR() S_ISREG() etc. But in some systems every file in the directory is displaying like a sub directory. and in some systems... (2 Replies)
Discussion started by: gandhevinod
2 Replies
Login or Register to Ask a Question