Bonjour,

Conséquent à une erreur inexpliquée, des milliers de fichiers ont été "corrompus" de la même manière.

Il s'agit de quelques caractères à ajouter ou supprimer pour réparer cela. A la mano, bonjour le taff.

Bref j'ai pondu une fonction qui parcourt un dossier de manière récursive.
Je voulais avoir votre avis concernant sa "propreté".

Voici la fonction en question et ci-après le code minimal d’exécution.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
void browse_directory(DIR *directory, char *path)
{
    directory = open_directory(path);                                   // Ouvrir le répertoire courant
    struct dirent *entity=NULL;                                         // Déclarer la structure de repertoire
    seekdir(directory,2);                                               // Déplacer le curseur de la structure du repetoire après . & ..
 
    while( (entity=readdir(directory)) != NULL  )                       // Tant que le repertoire courant n'a pas été entièrement parcouru
    {
        char *current_path = get_current_path(path, entity->d_name);    // On récupère le chemin de l'entité(fichier ou dossier) du repertoire courant
 
        if ( is_directory(current_path) != NULL )                       // Si l'entité est un dossier
        {
            printf("\nDOSSIER %s", current_path);                       
            browse_directory(directory, current_path);                  // Appel récursif de la fonction parcourir dossier
        }
 
        else printf("\nFILE %s", current_path);                         // Sinon c'est un fichier
    }
}
______________________________________________________________

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
 
#ifndef WIN32
    #include <sys/types.h>
    #define CLEAR "clear"
    /* system("clear") pour UNIX */
#else
    #define CLEAR "cls"
    /* system("cls") pour Windows */
#endif
 
DIR *open_directory(char * path)
{
    if ( opendir(path)==NULL )
        fprintf(stderr,"\nouverture dossier echoue : %s", path);
 
    return opendir(path);
}
 
void close_directory(DIR *directory)
{
    if ( closedir(directory)==-1 )
        fprintf(stderr,"\fermeture dossier echouee");
 
    closedir(directory);
}
 
void read_directory(DIR *directory, char *path)
{
    directory = open_directory(path);
    struct dirent *entity = NULL;
    while( (entity = readdir(directory)) != NULL )
    {
        printf("\n%s", entity->d_name);
    }
    close_directory(directory);
}
 
DIR *is_directory(char * path)
{
    return opendir(path);
}
 
char *get_current_path( char* path, char *next)
{
    char *current_path=(char*)malloc((strlen(path)+strlen(next)+2)*sizeof(char));
    int i;
    for (i=0; i<strlen(path);i++)
        current_path[i]=path[i];
 
    current_path[strlen(path)]='\\';
 
    for (i=strlen(path)+1; i<strlen(path)+strlen(next)+1;i++)
        current_path[i]=next[i-strlen(path)-1];
 
    current_path[strlen(path)+strlen(next)+1]='\0';
 
    return current_path;
}
 
void browse_directory(DIR *directory, char *path)
{
    directory = open_directory(path);                                   // Ouvrir le répertoire courant
    struct dirent *entity=NULL;                                         // Déclarer la structure de repertoire
    seekdir(directory,2);                                               // Déplacer le curseur de la structure du repetoire après . & ..
 
    while( (entity=readdir(directory)) != NULL  )                       // Tant que le repertoire courant n'a pas été entièrement parcouru
    {
        char *current_path = get_current_path(path, entity->d_name);    // On récupère le chemin de l'entité(fichier ou dossier) du repertoire courant
 
        if ( is_directory(current_path) != NULL )                       // Si l'entité est un dossier
        {
            printf("\nDOSSIER %s", current_path);                       
            browse_directory(directory, current_path);                  // Appel récursif de la fonction parcourir dossier
        }
 
        else printf("\nFILE %s", current_path);                         // Sinon c'est un fichier
    }
}
 
int main (void)
{
    char path[999]="C:\\Documents and Settings\\alv\\Mes documents";
    DIR* directory=NULL;
    browse_directory(directory,path);
    close_directory(directory);
    return 0;
}