Langage c : récupérer un dir
Fermé
mackoncef
Messages postés
20
Date d'inscription
mercredi 22 juin 2005
Statut
Membre
Dernière intervention
7 septembre 2006
-
28 juil. 2005 à 10:19
kilian Messages postés 8732 Date d'inscription vendredi 19 septembre 2003 Statut Non membre Dernière intervention 5 février 2025 - 29 juil. 2005 à 12:50
kilian Messages postés 8732 Date d'inscription vendredi 19 septembre 2003 Statut Non membre Dernière intervention 5 février 2025 - 29 juil. 2005 à 12:50
A voir également:
- C:\dir
- Langage ascii - Guide
- Langage binaire - Guide
- Pascal langage - Télécharger - Édition & Programmation
- Langage pascal - Télécharger - Édition & Programmation
- Dev-Pascal - Télécharger - Édition & Programmation
3 réponses
jipicy
Messages postés
40842
Date d'inscription
jeudi 28 août 2003
Statut
Modérateur
Dernière intervention
10 août 2020
4 897
28 juil. 2005 à 11:02
28 juil. 2005 à 11:02
Salut,
Tiens voilà 2 programmes tirés de cet ouvrage [http://www.editions-eyrolles.com/Livre/9782212091298/programmation-linux](les codes sources sont téléchargeables sur la page), qui traitent de ton problème et de l'emploi de "dirent.h" et autres commandes associées :
1- /
Tiens voilà 2 programmes tirés de cet ouvrage [http://www.editions-eyrolles.com/Livre/9782212091298/programmation-linux](les codes sources sont téléchargeables sur la page), qui traitent de ton problème et de l'emploi de "dirent.h" et autres commandes associées :
1- /
/* We start with the appropriate headers and then a function, printdir, which prints out the current directory. It will recurse for subdirectories, using the depth parameter is used for indentation. */ #include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> void printdir(char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); return; } chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { /* Found a directory, but ignore . and .. */ if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue; printf("%*s%s/\n",depth,"",entry->d_name); /* Recurse at a new indent level */ printdir(entry->d_name,depth+4); } else printf("%*s%s\n",depth,"",entry->d_name); } chdir(".."); closedir(dp); } /* Now we move onto the main function. */ int main() { printf("Directory scan of /home:\n"); printdir("/home",0); printf("done.\n"); exit(0); }2- /
/* We start with the appropriate headers and then a function, printdir, which prints out the current directory. It will recurse for subdirectories, using the depth parameter is used for indentation. */ #include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> void printdir(char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s\n", dir); return; } chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { /* Found a directory, but ignore . and .. */ if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) continue; printf("%*s%s/\n",depth,"",entry->d_name); /* Recurse at a new indent level */ printdir(entry->d_name,depth+4); } else printf("%*s%s\n",depth,"",entry->d_name); } chdir(".."); closedir(dp); } /* Now we move onto the main function. */ int main(int argc, char* argv[]) { char *topdir, pwd[2]="."; if (argc != 2) topdir=pwd; else topdir=argv[1]; printf("Directory scan of %s\n",topdir); printdir(topdir,0); printf("done.\n"); exit(0); }Le 1er programme est statique au niveau du répertoire à explorer, le second est dynamique, emploi :
programme /chemin_rep/a_explorer | moreM'en demande pas plus (je pompe rien en C), j'avais ça sous la main qui semblait correspondre à ta demande ;-))
kilian
Messages postés
8732
Date d'inscription
vendredi 19 septembre 2003
Statut
Non membre
Dernière intervention
5 février 2025
1 526
29 juil. 2005 à 12:50
29 juil. 2005 à 12:50
Salut,
Si tu veux j'ai fait une fonction il ya deux jours qui affiche chaque fichiers avec son chemin dans une arborescence.
C'est peut être un plus court:
Ya juste à bidouiller un ptit peu pour afficher comme tu voulais le faire.
Si tu veux j'ai fait une fonction il ya deux jours qui affiche chaque fichiers avec son chemin dans une arborescence.
C'est peut être un plus court:
// Determine si le nom d'un rep se refere à un rep valide int is_dir(char *path) { DIR *is_folder=NULL; is_folder=opendir(path); if (is_folder!=NULL){ closedir(is_folder); return 1; } closedir(is_folder); return 0; } int scan_folder(char *dirname) { DIR *folder; struct dirent *entity; char *real_path=NULL; folder=opendir(dirname); if (folder==NULL) return -1; while (entity=readdir(folder)){ if(real_path!=NULL){ free(real_path); real_path=NULL; } if(!strcmp(entity->d_name,".") || !strcmp(entity->d_name,"..") ) //Evite quelques problemes sous Unix/Linux continue; real_path=(char *)malloc( strlen(dirname) + strlen(entity->d_name) + 2);/* Si c'est+1 au lieu de +2, j'ai parfois un problème de segmentation. Sais pas pourquoi .. */ sprintf(real_path,"%s/%s",dirname,entity->d_name); if( is_dir(real_path) == 1 ){ scan_folder(real_path); } else printf("Fichier: %s\n",real_path); } closedir(folder); if (real_path!=NULL){ free(real_path); real_path=NULL; } return 0; }
Ya juste à bidouiller un ptit peu pour afficher comme tu voulais le faire.
mackoncef
Messages postés
20
Date d'inscription
mercredi 22 juin 2005
Statut
Membre
Dernière intervention
7 septembre 2006
1
28 juil. 2005 à 14:07
28 juil. 2005 à 14:07
Merci,
cette fonction printdir correspond a peu près a ce que je cherche avec quelques modificcations
cette fonction printdir correspond a peu près a ce que je cherche avec quelques modificcations