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
| int main (void)
{
short nombreCommandes=-1;
struct command *commands = malloc(1*sizeof(struct command));
if (commands == NULL) // On vérifie si l'allocation a marché ou pas
{
exit(EXIT_FAILURE);// On arrête tout
}
commands=GetCommandesString("fichier.par",commands,&nombreCommandes);
}
struct command * GetCommandesString( char * file, struct command *commands, short * nombreCommands)
{
FILE * fp = fopen( file , "r" );
char * aux;
int numeroCommande=0;
memset(buffer, 0, sizeof(buffer));
while(fgets( buffer , sizeof( buffer ) , fp ))
{
if(*nombreCommands==-1)
{*nombreCommands=0;}
else
{
(*nombreCommands)++;
numeroCommande++;
commands = (struct command *)realloc(commands,((*nombreCommands)+1)*sizeof(struct command));
}
if(!strncmp( buffer, "arg",3)&&(*nombreCommands!=-1))
{
if(strlen(buffer)) buffer[strlen( buffer) - 1 ] = 0;
aux = strchr( buffer , '=' );
aux=trim(aux+1);
strcpy(commands[numeroCommande].arg, aux);
printf("NV ARG de commande: %s \n", commands[numeroCommande].arg);
}
}
fclose( fp );
return commands;
} |
Partager