Le code ci-dessous me permet d'afficher tous les fichiers d'un répertoire (FAQ C). Comment faire pour n'afficher que les fichiers d'un même format, par exemple mp3. A quel endroit du code puis-je le specifier et comment ?
Merci
Version imprimable
Le code ci-dessous me permet d'afficher tous les fichiers d'un répertoire (FAQ C). Comment faire pour n'afficher que les fichiers d'un même format, par exemple mp3. A quel endroit du code puis-je le specifier et comment ?
Merci
1° A l'heure où ke poste, il n'y a pas de "code ci-dessous".
2° Tu compares l'extension du fichier avec .mp3 (de préférence avec une fonction qui ignore la casse) et tu n'affiches pas si ce n'est pas bon...
excuse, voilà le code :Une fonction qui ignore la casse, c'est à dire ?Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <stdio.h> #include <sys/types.h> #include <dirent.h> int main(void) { struct dirent *lecture; DIR *rep; rep = opendir("/tmp"); while ((lecture = readdir(rep))) { printf("FICHIER: %s\n", lecture->d_name); } closedir(rep); return 0; }
Merci
Généralement, elle s'appelle stricmp() ou strcasecmp(). (Ces fonctions ne sont pas standard).
Et pour trouver où commence l'extension, un petit strrchr(lecture->d_name, '.') devrait suffire.
J'ai lu sur le forum que tu pouvais le faire en utilisant :
- Boost.Filesystem // assez portable
- l'API système
++
Pas du C...Citation:
Envoyé par Colbix
Oups vui, sorry ;)Citation:
Envoyé par Emmanuel Delahaye
Citation:
Envoyé par pierrOPSG
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 /* standard C */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> /* POSIX.1 */ #include <sys/types.h> #include <dirent.h> static void str_tolower(char *s) { while (*s) { *s = tolower(*s); s++; } } int main(void) { #define dname "/Documents and Settings/Papou/Mes documents/Ma musique\ /Itunes/Itunes music/Josephine Marsh/Josephine Marsh" DIR *rep = opendir(dname); if (rep != NULL) { struct dirent *lecture; while ((lecture = readdir(rep))) { char *fname = strdup (lecture->d_name); if (fname != NULL) { str_tolower(fname); if (strstr(lecture->d_name, ".mp3") != NULL) { printf("FICHIER: %s\n", lecture->d_name); } free (fname),fname=NULL; } } closedir(rep); } else { perror (dname); } return 0; }
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 FICHIER: 01 The Boogie_The Cocktail Reels.mp3 FICHIER: 02 Cape Breton_Dancing Master [Jigs].mp3 FICHIER: 03 Tony Dalton's_Tay in a Bucket (Ho.mp3 FICHIER: 04 Paddy's Polkas.mp3 FICHIER: 05 Matthew's Waltz.mp3 FICHIER: 06 John Naughton's_Return to Camden.mp3 FICHIER: 07 Bold Doherty_Northern [Jigs].mp3 FICHIER: 08 Bainis_Michael Joe Kennedy's (Hor.mp3 FICHIER: 09 The Shepherd - Slow Air.mp3 FICHIER: 10 Tommy Mulhare's_East of Glendart.mp3 FICHIER: 11 Lad O Beirne's_Phyllis' Birthday.mp3 FICHIER: 12 O Carolan's Reciept [Planxty].mp3 FICHIER: 13 Wallop the Cat from Under the Tab.mp3 FICHIER: 14 Raitlin Island_Michael Joe Kenned.mp3 Press ENTER to continue.