Fichier CSV (problème de code)
Bonsoir, je bloque sur un programme permettant d'afficher la liste des login des utilisateurs qui utilisent bash comme interpréteur de commandes, pour cela on a disposition une fonction char** strsplit(char *s, char sep) qui découpe une chaîne de caractères s grâce au caractère séparateur (sep) et qui retourne un tableau dynamique des sous-chaînes. Ce tableau se termine par le pointeur NULL, à la manière du paramètre env du main(). Par exemple : strsplit("a:b:cd",':') ---->
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 53 54 55 56 57
|
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main (int argc,char *argv[]){
char **strsplit(const char *s, const char sep){
if(s==NULL)
return NULL;
int nbsep=0; /* nb de séparateurs */
for(int i=0;s[i]!='\0';i++){
if (s[i]==sep)
nbsep++;
}
//printf("nbsep = %d\n",nbsep);
char **res=malloc((nbsep+2)*sizeof(char*)); /* allocation du résultat */
int n=0;
int i=0;
int APRESSEP=0; /* faux */
do{
int l=0;
// lng de la sous-chaîne
while(s[i]!='\0' && s[i]!=sep){
l++;
i++;
}
res[n]=malloc(l+1);
strncpy(res[n], s+i-l, l);
res[n][l]='\0';
n++;
APRESSEP=0;
if(s[i]==sep){
i++;
APRESSEP=1;
}
} while(s[i]!='\0');
if(APRESSEP){ /* sep suivi de fin de chaîne */
//printf("n=%d ; i=%d\n",n,i);
res[n]=malloc(1);
res[n][0]='\0';
n++;
}
res[n]=NULL;
return res;
}
FILE *fichier=fopen("/ect/passwd","r");
char ligne[TAILLE];
char tch**=strsplit(ligne,":");
while(!feof(fichier)){
if(fgets(ligne,TAILLE,fichier)){
if((strcmp(tch[n-1],"/bin/bash")==0)){
printf("%s",tch[0]);
}
}
}
fclose(fichier);
} |
Voila le fichier fournis :
root:x:0:0:root:/root:/bin/bash
dupont:x:36:36:Pierre Dupont:/home/dupont:/bin/bash
martin:x:42:42:Pierre Martin:/home/martin:/bin/tcsh
inscription:x:500:500:inscription:/home/inscription:xinit
Résulat --> root dupond
J'ai vraiment besoin de votre aide s'il vous plait merci d'avance