Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

scandir

22 réponses
Avatar
matt
Bonjour,

J'utilise la fonction scandir pour lister un repertoire comme suit :

n = scandir(".", &filelist, 0, alphasort);

mais je voudrais mettre un filtre du style *.txt mais je ne vois pas
comment faire ?

Ca se passe au niveau du 3eme paramètres, un pointeur de fonction mais
pour la suite ???

Pourriez vous m'aider ?

Merci pour vos réponses,

Matt.

PS : si HS pouvez vous m'indiquer le bon groupe.

10 réponses

1 2 3
Avatar
PulkoMandy
Le Sat, 21 Apr 2007 14:56:25 +0200, matt a écrit:

Le troisième paramètre est une fonction qui sera apellée pour chaque
fichier. Dans cette fonction, tu dois regarder si le fichier est bien un
.txt (comparaison du nom), et retourner 1 si c'est le cas, 0 sinon.
La strcture dirent passée en param à cette fonction contient le nom du
fichier, donc rien de bien compliqué.
Avatar
matt
Le Sat, 21 Apr 2007 14:56:25 +0200, matt a écrit:

Le troisième paramètre est une fonction qui sera apellée pour chaque
fichier. Dans cette fonction, tu dois regarder si le fichier est bien un
..txt (comparaison du nom), et retourner 1 si c'est le cas, 0 sinon.
La strcture dirent passée en param à cette fonction contient le nom du
fichier, donc rien de bien compliqué.





Merci pour la reponse, voici donc la fonction qui me permet de filtrer
les "*.txt" :

#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int filter(const struct dirent *a)
{
char *name = strrchr(a->d_name, '.');

if(name == NULL)
return 0;

if(strcmp(name, ".txt") == 0)
return 1;

return 0;
}

int main()
{
struct dirent **filelist = NULL;
int n = 0, i = 0;

n = scandir(".", &filelist, filter, alphasort);
if (n < 0)
{
perror("SCANDIR");
exit(EXIT_FAILURE);
}

for(i= 0; i < n; i++)
{
printf("%sn", filelist[i]->d_name);
free(filelist[i]);
}

free(filelist);

exit(EXIT_SUCCESS);
}

qu'en penser vous ?

Matt...

Avatar
Xavier Roche
Merci pour la reponse, voici donc la fonction qui me permet de filtrer
les "*.txt" :
char *name = strrchr(a->d_name, '.');
(..)

if(strcmp(name, ".txt") == 0)
return 1;


Humm..
foo.bar.txt
^

Quelque chose comme:

int filter(const struct dirent *a) {
const char *s, *dot;
for(s = a->d_name, dot = NULL ; *s != '' ; s++) {
if (*s == '.') {
dot = s;
}
}
return ( dot != NULL && strcasecmp(dot, ".txt") == 0 ) ? 1 : 0;
}

serait mieux amha.

Avatar
espie
In article <f0d6a5$2vp$,
Xavier Roche wrote:
Quelque chose comme:

int filter(const struct dirent *a) {
const char *s, *dot;
for(s = a->d_name, dot = NULL ; *s != '' ; s++) {
if (*s == '.') {
dot = s;
}
}
return ( dot != NULL && strcasecmp(dot, ".txt") == 0 ) ? 1 : 0;
}


Je prefere utiliser plus la bibliotheque standard. Par exemple:
int
filter(const struct dirent *a)
{
const char *n = a->d_name;
size_t len = strlen(n);
return len >= 4 && strcmp(n+len-4, ".txt") == 0;
}

(ou bien un strrchr, hein, ca marche aussi, mais uniquement parce qu'on
cherche .txt).

Avatar
Harpo
On Sat, 21 Apr 2007 16:03:21 +0200, matt wrote:


if(strcmp(name, ".txt") == 0)
return 1;



Ouille aïe aïe aïlle.

int is_txt(const char * name)
{
size_t len:
len = strlen(name);
if (len < 4) return 0;
if (memcmp(name + len - 4, ".txt", 4)) return 1;
return 0:
}

Bon :
Pas compilé, pas testé, mais ça devrait le faire au bout d'un certain
temps. Le jour où j'y penserai, je l'étendrai aux '.mp3', il y a plus de
marché.

--
SKILL (Simple Knowledge Inference Logic Language)
http://patrick.davalan.free.fr/skill-0/

Avatar
Xavier Roche
if (memcmp(name + len - 4, ".txt", 4)) return 1;


FOO.TXT ?

Avatar
Harpo
On Sat, 21 Apr 2007 17:04:30 +0200, Xavier Roche wrote:

if (memcmp(name + len - 4, ".txt", 4)) return 1;


FOO.TXT ?


On s'en foo !

--
SKILL (Simple Knowledge Inference Logic Language)
http://patrick.davalan.free.fr/skill-0/


Avatar
Harpo
On Sat, 21 Apr 2007 17:06:20 +0200, Harpo wrote:

FOO.TXT ?


On s'en foo !


Point bar !

--
SKILL (Simple Knowledge Inference Logic Language)
http://patrick.davalan.free.fr/skill-0/


Avatar
matt
Merci pour la reponse, voici donc la fonction qui me permet de filtrer
les "*.txt" :
char *name = strrchr(a->d_name, '.');
(..)

if(strcmp(name, ".txt") == 0)
return 1;


Humm..
foo.bar.txt
^

Quelque chose comme:

int filter(const struct dirent *a) {
const char *s, *dot;
for(s = a->d_name, dot = NULL ; *s != '' ; s++) {
if (*s == '.') {
dot = s;
}
}
return ( dot != NULL && strcasecmp(dot, ".txt") == 0 ) ? 1 : 0;
}

serait mieux amha.


ok, mais je fais un strrchr donc on commence la recherche par la fin
donc c'est bon ?

Matt.


Avatar
matt
On Sat, 21 Apr 2007 16:03:21 +0200, matt wrote:

if(strcmp(name, ".txt") == 0)
return 1;



Ouille aïe aïe aïlle.



Pourquoi ?

Sachant que c'est moi qui maitrise l'écriture, donc le nom et
l'extension de mes fichiers donc la casse aussi.

Matt.


1 2 3