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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void erreur_memoire(void)
{
printf("Erreur Mémoire\n");
exit(1);
}
void info_fichier(const char *fichier, int *nb_lignes, int *nb_colonnes, int *nb_lignes_vides, char separateur, char separateur_texte)
{
char c;
int nb_sep_total;
FILE *f;
if ( f=fopen(fichier,"rt") )
{
c=fgetc(f);
nb_sep_total=0;
*nb_lignes=0;
*nb_colonnes=1;
while( c!=EOF )
{
if ( c==separateur && *nb_lignes==0 && nb_colonnes )
(*nb_colonnes)++;
if ( c=='\n' && nb_lignes )
(*nb_lignes)++;
if ( c==separateur && nb_lignes )
nb_sep_total++;
c=fgetc(f);
}
if (nb_lignes_vides)
*nb_lignes_vides=((*nb_colonnes-1)*(*nb_lignes)-nb_sep_total)/(*nb_colonnes-1);
fclose(f);
}
}
void printf_tableau(int nb_lignes, int nb_colonnes, double **tableau)
{
int i,j;
for(i=0;i<nb_lignes;i++)
{
for(j=0;j<nb_colonnes;j++)
printf("%.1lf ",tableau[i][j]);
printf("\n");
}
}
/* renvoie -1 si la ligne est vide 0 sinon */
int enregistre_ligne(double **sortie, char *buffer,int ligne_courante, int nb_lignes, int nb_colonnes, char separateur, char separateur_texte)
{
int nb_sep=0;
char *courant_debut=buffer, *courant_fin=NULL, *nombre;
size_t lg;
while(nb_sep!=nb_colonnes-1)
{
if ( courant_fin=strchr(courant_debut,separateur) )
{
lg=sizeof(courant_fin-1-courant_debut);
if ( !(nombre=(char *)malloc(lg)) )
erreur_memoire();
strncpy(nombre,courant_debut, lg);
if (strlen(nombre))
sortie[ligne_courante][nb_sep]=atof(nombre);
else
return -1;
courant_debut=courant_fin+sizeof(char);
nb_sep++;
free(nombre);
}
else
{
sortie[ligne_courante][nb_sep]=0.0;
nb_sep++;
}
}
if ( courant_fin=strchr(courant_debut,'\0') )
{
lg=sizeof(courant_fin-1-courant_debut);
if ( !(nombre=(char *)malloc(lg)) )
erreur_memoire();
strncpy(nombre,courant_debut, lg);
if (strlen(nombre))
sortie[ligne_courante][nb_sep]=atof(nombre);
else
sortie[ligne_courante][nb_sep]=0.0;
free(nombre);
}
else
{
sortie[ligne_courante][nb_sep]=0.0;
nb_sep++;
}
return 0;
}
/* retourne -1 si erreur 0 sinon */
double **lecture_cvs(const char *fichier, int nbl, int nbc, char separateur, char separateur_texte)
{
FILE *f;
int ligne, i, nb_char;
char *buf,c;
double **sortie;
/* On alloue le tableau de sortie */
/* les colonnes */
if ( !(sortie=(double **)malloc(sizeof(double)*nbl)) )
erreur_memoire();
/* les lignes */
for (i=0;i<nbl;i++)
if ( !(sortie[i]=(double *)malloc(sizeof(double)*nbc)) )
erreur_memoire();
if ( f=fopen(fichier,"rt") )
{
/* On initialise la ligne courante du tableau */
ligne=0;
for(i=0;i<nbl;i++)
{
c=fgetc(f);
nb_char=1;
while(c!='\n' && c!=EOF)
{
c=fgetc(f);
nb_char++;
}
if ( !(buf=(char *)malloc(sizeof(char)*(nb_char+1))) )
exit(1);
fseek(f,-nb_char,SEEK_CUR);
fscanf(f,"%s\n",buf); /* on récupére dans le buffer la ligne courante */
/* on écrit la ligne contenue dans le buffer dans le tableau de sortie.
Si la ligne est vide on n'imcrémente pas ligne donc on réécrira dessus au prochain tour de boucle */
if (enregistre_ligne(sortie,buf,ligne,nbl,nbc, separateur, separateur_texte)!=-1)
ligne++;
free(buf);
}
fclose(f);
}
else
return NULL;
return sortie;
}
void libere_tableau(int nb_lignes, double **tableau)
{
int i;
for(i=0;i<nb_lignes;i++)
free(tableau[i]);
free(tableau);
}
int main(int argc, char **argv)
{
double **tableau;
char sep=',', texte='\"';
int nbl,nbc,nblv;
info_fichier("fichier.csv",&nbl,&nbc,&nblv,sep,texte);
printf("%d-%d-%d\n",nbl,nbc,nblv);
nbl=nbl-nblv;
tableau=lecture_cvs("fichier.csv",nbl,nbc,sep,texte);
printf_tableau(nbl,nbc,tableau);
libere_tableau(nbl,tableau);
return EXIT_SUCCESS;
} |
Partager