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
| void lire(int num){
// PROBLEME : CB Y A TIL DE FICHIERS DS LE DOSSIER AFIN DE COMPARER AVEC NUM ---> nbfiles()
// PB2 : Segmentation fault ?
int cmp = nbfiles("home/larchicha/Desktop/recus");
int i ; int descript ;
char * num_str = malloc(50*sizeof(char));
char * buff = malloc(100*sizeof(char));
char * chemin = malloc(100*sizeof(char));;
strcpy(chemin,"home/larchicha/Desktop/recus/");// chemin contient le debut du chemin moins le nom du fichier (1 2 3 4)
for(i=0;i<cmp;i++){
if (i == num){// tant qu on trouve pas le numero du symlink on boucle
snprintf(num_str, 4, "%d", num);// convertit num : int -> string
strcat(chemin,num_str);// concat le nom du fichier (1 ou 2 ou 3 ou 4) au chemin
descript = open(chemin,O_RDONLY);//on ouvre le fichier appeller 'num'
read(descript,buff,sizeof buff);// on lit le contenu du fichier
printf("Le contenu du message est : %s\n",buff);//on l affiche
} else {
//perror("Le numero donne en argument ne correspond a aucun fichier : ");
}
}
}
int main(){
nbfiles("recus");
lire(2);
return 0;
} |
Partager