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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define TAILLE_MAX 128 // Tableau de taille 128
#define VRAI 1
#define FAUX 0
typedef struct str_fichier {
char *nom;
FILE* p_fic;
int longueur;
} type_fic, *p_type_fic;
int main(int argc, char * argv[]) {
unsigned char buffer[TAILLE_MAX + 1]; //tabeau avec le 129 /0 pour l'affichage
// FILE* file;
int i, c, nb_blocks, count;
int taille_chaine;
// allocation des deux str_fichier
type_fic fic_lu = { NULL, NULL, 0 };
type_fic fic_ecr = { NULL, NULL, 0 };
p_type_fic p_fic_lu = &fic_lu;
p_type_fic p_fic_ecr = &fic_ecr;
if (argc == 2) {
// recupératon du fichiercomme paramètre
taille_chaine = strlen(argv[1]);
p_fic_lu->nom = malloc(taille_chaine * sizeof(char));
memset(p_fic_lu->nom, 0, taille_chaine);
strcpy(p_fic_lu->nom, argv[1]);
// strcpy(p_fic_lu->nom , "C:\\Users\\moi\\Documents\\Exo0001\\AM022-203248-20161227145845")
} else {
printf("Problème de paramètre \n");
return EXIT_FAILURE; //fin de /*traitement*/
}
taille_chaine = p_fic_lu->longueur + 5;
//printf(" longeur %d \n", taille_chaine);
p_fic_ecr->nom = malloc(taille_chaine * sizeof(char));
memset(p_fic_ecr->nom, 0, p_fic_ecr->longueur);
strcpy(p_fic_ecr->nom, p_fic_lu->nom);
// printf("fic %s\n",p_fic_ecr->nom);
strcat(p_fic_ecr->nom, "_OUT");
printf("Fichier en lecture %s \n\n", p_fic_lu->nom);
printf("Fichier en écriture %s \n\n", p_fic_ecr->nom);
p_fic_lu->p_fic = fopen(p_fic_lu->nom, "r");
//file = fopen(p_fic_lu->nom, "r");
//if (file == NULL) {
if (p_fic_lu->p_fic == NULL) {
printf("Absence du fichier %s ou problèmede lecture \n\n",
p_fic_lu->nom);
free(p_fic_lu->nom);
free(p_fic_ecr->nom);
return EXIT_FAILURE; //fin de traitement
}
p_fic_ecr->p_fic = fopen(p_fic_ecr->nom, "w") ; //fichier d ecriture
if (p_fic_ecr->p_fic == NULL) {
printf("Absence du fichier %s ou problèmede lecture \n\n", p_fic_ecr->nom);
free(p_fic_lu->nom);
free(p_fic_ecr->nom);
fclose(p_fic_lu->p_fic);
return EXIT_FAILURE; //fin de traitement
}
count = 0;
nb_blocks = 0; //
while (1) {
c = fgetc(p_fic_lu->p_fic);
if (c == EOF || count == TAILLE_MAX) {
buffer[count] = '\0';
++nb_blocks;
if (buffer[0] == '2') {
if (buffer[35] == 'A') {
printf("Block A2%d:\n%s\n\n", nb_blocks, buffer);
/* vidage 119-128 10A*/
for (i = 118; i <= 127; ++i)
buffer[i] = ' ';
printf("PostBlock A2%d:\n%s\n\n", nb_blocks, buffer);
}
}
//printf("Block %d:\n%s\n\n", nb_blocks, buffer);
if (c == EOF)
break;
count = 0;
}
buffer[count] = c; // cast implicite
count++;
}
fclose(p_fic_ecr->p_fic); //ferme le fichier
fclose(p_fic_lu->p_fic); //ferme le fichier
//dealloaction des structures str_fichier
free(p_fic_lu->nom);
free(p_fic_ecr->nom);
return EXIT_SUCCESS;
} |
Partager