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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* fonction qui traite les lignes */
/* char* sub_string(char* entree,int deb, int fin) */
/* est une fonction qui ressort les caracteres de entree[deb] à entree[fin] */
char *sub_string (char *entree, int deb, int fin)
{
assert (fin >= deb);
assert ((int)strlen (entree) >= fin);
entree[fin] = 0;
return entree + deb;
}
void traite_ligne (char *ligne, char const *date)
{
char nomfic[16];
nomfic[sizeof nomfic - 1] = 0;
strcpy (nomfic, sub_string (ligne, 0, 2));
assert (nomfic[sizeof nomfic - 1] == 0);
strcat (nomfic, "-");
assert (nomfic[sizeof nomfic - 1] == 0);
strcat (nomfic, date);
assert (nomfic[sizeof nomfic - 1] == 0);
strcat (nomfic, ".txt");
assert (nomfic[sizeof nomfic - 1] == 0);
{
FILE *fichier = fopen (nomfic, "a");
if (fichier != NULL)
{
printf ("nomfic*'%s'\n", nomfic);
fprintf (fichier, "%s\n", sub_string (ligne, 4, 6));
fclose (fichier);
}
else
{
perror (nomfic);
}
}
}
/* fonction qui traite un fichier chemin est le chemin complet du fichier et */
/* date est la date du lancement du programme */
int traite_fichier (char const *chemin, char const *date)
{
int err = 0;
FILE *fic = fopen (chemin, "r");
if (fic != NULL)
{
/* on appelle ici la fonction traite_ligne en boucle jusqu'à ce qu'on ait plus */
/* de ligne */
char ligne[256];
while (fgets (ligne, sizeof ligne, fic) != NULL)
{
traite_ligne (ligne, date);
}
printf ("\n");
printf ("%s a ete traite\n", chemin);
fclose (fic);
}
else
{
printf ("Erreur de lecture du fichier\n");
err = 1;
}
return err;
}
int main (void)
{
traite_fichier ("test.txt", "01/01/07");
return 0;
} |
Partager