| View previous topic :: View next topic |
| Author |
Message |
ne0h
Joined: 21 Feb 2008 Posts: 386
|
Posted: Thu Mar 27, 2008 6:42 am Post subject: [SOLVED]Alphasort File listed |
|
|
I've this code to list all file in a directory and alphasort the file printed, but the file are listed in the same position as the directory, but i want list the file with this sequence:
.
..
DIR
dir
FILE.EXT
file.ext
The scandir function library:
| Code: |
#include<dirent.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
int scandir(const char *dir, struct dirent ***namelist,
int (*select)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **))
{
DIR *d;
struct dirent *entry;
register int i=0;
size_t entrysize;
if ((d=opendir(dir)) == NULL)
return(-1);
*namelist=NULL;
while ((entry=readdir(d)) != NULL)
{
if (select == NULL || (select != NULL && (*select)(entry)))
{
*namelist=(struct dirent **)realloc((void *)(*namelist),
(size_t)((i+1)*sizeof(struct dirent *)));
if (*namelist == NULL) return(-1);
entrysize=sizeof(struct dirent)-sizeof(entry->d_name)+strlen(entry->d_name)+1;
(*namelist)[i]=(struct dirent *)malloc(entrysize);
if ((*namelist)[i] == NULL) return(-1);
memcpy((*namelist)[i], entry, entrysize);
i++;
}
}
if (closedir(d)) return(-1);
if (i == 0) return(-1);
if (compar != NULL)
qsort((void *)(*namelist), (size_t)i, sizeof(struct dirent *), compar);
return(i);
}
int alphasort(const struct dirent **a, const struct dirent **b)
{
return(strcmp((*a)->d_name, (*b)->d_name));
}
|
Has anyone a solution?
Tanks in advance...
Last edited by ne0h on Fri Mar 28, 2008 6:21 am; edited 1 time in total |
|
| Back to top |
|
 |
Insert_witty_name
Joined: 10 May 2006 Posts: 376
|
Posted: Thu Mar 27, 2008 8:26 am Post subject: |
|
|
| qsort + google = win. |
|
| Back to top |
|
 |
kuroneko
Joined: 08 Dec 2005 Posts: 24 Location: Chigasaki, Japan
|
Posted: Thu Mar 27, 2008 10:14 am Post subject: Re: Alphasort File listed |
|
|
| ne0h wrote: | | Has anyone a solution? |
Just sorting by name isn't going to get you anywhere as readdir() does not guarantee any order for the items returned. When you sort you'll have to consider type (dir/file) first, then name.
IIRC, stat() requires an absolute file name so once you read the entry with readdir() insert some function which obtains the type then pass this and the actual name to the comparator. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Thu Mar 27, 2008 3:48 pm Post subject: |
|
|
Well thanks for putting me onto qsort. I never knew about it.
I used this to sort an array of strings (file paths) for my mp3 browser:
http://www.c.happycodings.com/Sorting_Searching/code16.html
(already had the strings in memory of course).
Doesn't help with the thread starters' problem sorry.
Art. _________________ If not actually, then potentially. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Fri Mar 28, 2008 12:39 am Post subject: |
|
|
Funny one of my first botched attempts treats the path strings as integers or something,
and is a good way to seemingly randomise an mp3 playlist without copying into a duplicate
buffer or anything, then you can just alpha sort them properly again.
What a bonus :) But it's the same every time,
so I think I'll go for randomising the selection numbers and go from there. _________________ If not actually, then potentially. |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Fri Mar 28, 2008 4:15 am Post subject: |
|
|
| I knew I wasn't dealing with many files, so I just did my own simple bubble sort in Doom's file requester. It sorts directories first, and everything alphabetically. Sorting is one of those things you get like the first week of any programming class. It's not a big deal. :) |
|
| Back to top |
|
 |
|