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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 1er argument --> nom du fichier
// 2ème argument --> nombre de lignes
// 3ème argument --> nombre de colonnes
// 4ème argument --> taille maxi d'une ligne
int main(int argc, char *argv[]){
//Déclaration des variables
int nb_ligne=atoi(argv[2]);
int nb_colonne=atoi(argv[3]);
int maxiligne=atoi(argv[4]);
int i;
char *split;
float **tab;
FILE* entree;
char *ligne;
int line=0;
int columm=0;
char *p;
char *sep ="\t";
char tab2[20];
//Allocation de la taille maxi d'une ligne
ligne = (char*) malloc ( sizeof(char) * maxiligne);
//Allocation de la matrice
/* Allocation de la 1er dimension */
tab = malloc ( sizeof(*tab) * nb_ligne);
/* Allocation des tableaux */
for (i=0; i<nb_ligne; i++)
{
tab[i] = malloc ( sizeof(**tab) * nb_colonne);
}
//Ouverture du fichier de données
if((entree= fopen(argv[1], "r")) == NULL) {
return EXIT_FAILURE;
}
//tant qu'il y a une ligne
while(fgets(ligne, sizeof ligne, entree)){
columm=0;
//On vire les retour chariot
p = strchr (ligne, '\n');
if (p)
{
*p = 0;
}
//on split et on remplit le tableau (tab)
printf ("%s",ligne);
*tab[line] = str_split (*ligne,*sep);
for(i=0;i<5;i++)
{
printf("%s",tab2[i]);
}
//split = strtok(ligne, sep);
//while ( split != NULL ) {
//tab[line][columm]=(float)split;
//printf("token: %10s\n", split);
// split = strtok(NULL, sep);
// columm++;
//}
//on incremente la ligne pour le tableau
line++;
}
return 0;
}
//LE SPLIT DE PERL
char **str_split (char *s, const char *ct)
{
char **tab = NULL;
if (s && ct)
{
int i;
char *cs = NULL;
size_t size = 1;
/* (1) */
for (i = 0; (cs = strtok (s, ct)); i++)
{
if (size <= i + 1)
{
void *tmp = NULL;
/* (2) */
size <<= 1;
tmp = realloc (tab, sizeof (*tab) * size);
if (tmp)
{
tab = tmp;
}
else
{
fprintf (stderr, "Memoire insuffisante\n");
free (tab);
tab = NULL;
exit (EXIT_FAILURE);
}
}
/* (3) */
tab[i] = cs;
s = NULL;
}
tab[i] = NULL;
}
return tab;
} |
Partager