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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define LenA 4
# define LenM 2
typedef struct
{
char *Nom;
int Ng, Neg, Nhg;
}
Activite;
typedef struct
{
char const *Mod, *Sem, *Prom, *Cord;
Activite act[LenA];
}
Module;
static void Lecturefichier (FILE * f, Module t[])
{
char *pointeur;
char *buffer;
char ch[80];
char const *separateur = { ";" };
int i = 1;
int valeur;
int j = 1;
t[1].Mod = "AP11";
while ((fgets (ch, 80, f)) != NULL)
{
buffer = strdup (ch);
pointeur = strtok (buffer, separateur);
if ((strcmp (pointeur, t[i].Mod)) == 0)
{
t[i].Mod = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].Sem = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].Prom = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].Cord = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].act[j].Nom = strdup (pointeur);
pointeur = strtok (NULL, separateur);
sscanf (pointeur, "%d", &valeur);
t[i].act[j].Ng = valeur;
pointeur = strtok (NULL, separateur);
sscanf (pointeur, "%d", &valeur);
t[i].act[j].Neg = valeur;
pointeur = strtok (NULL, separateur);
sscanf (pointeur, "%d", &valeur);
t[i].act[j].Nhg = valeur;
j++;
}
else
{
i++;
t[i].Mod = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].Sem = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].Prom = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].Cord = strdup (pointeur);
pointeur = strtok (NULL, separateur);
t[i].act[j].Nom = strdup (pointeur);
pointeur = strtok (NULL, separateur);
sscanf (pointeur, "%d", &valeur);
t[i].act[j].Ng = valeur;
pointeur = strtok (NULL, separateur);
sscanf (pointeur, "%d", &valeur);
t[i].act[j].Neg = valeur;
pointeur = strtok (NULL, separateur);
sscanf (pointeur, "%d", &valeur);
t[i].act[j].Nhg = valeur;
}
}
}
static void Ecriturefichier (Module t[])
{
char nomfich[21];
int i, j;
FILE *sortie;
printf ("nom du fichier à créer : ");
scanf ("%20s", nomfich);
sortie = fopen (nomfich, "w");
for (i = 1; i < LenM; i++)
{
for (j = 1; j < LenA; j++)
{
fputs (t[i].Mod, sortie);
fputc (';', sortie);
fputs (t[i].Sem, sortie);
fputc (';', sortie);
fputs (t[i].Prom, sortie);
fputc (';', sortie);
fputs (t[i].Cord, sortie);
fputc (';', sortie);
fputs (t[i].act[j].Nom, sortie);
fputc (';', sortie);
putw (t[i].act[j].Ng, sortie);
fputc (';', sortie);
putw (t[i].act[j].Neg, sortie);
fputc (';', sortie);
putw (t[i].act[j].Nhg, sortie);
fputc (';', sortie);
}
}
fclose (sortie);
}
int main (void)
{
int ret = EXIT_SUCCESS;
Module exemple[LenM];
FILE *f = fopen ("donnees.csv", "r");
if (f != NULL)
{
Lecturefichier (f, exemple);
fclose (f);
Ecriturefichier (exemple);
}
else
{
printf ("erreur : le fichier ne s'ouvre pas \n");
ret = EXIT_FAILURE;
}
return ret;
} |
Partager