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
|
/* Programme : Coder.c
* Syntaxe : coder [nomfichier] [action]
* nomfichier est le nom du fichier pour les données codées
* action est égale à D pour coder ,ou n'importe quel
* autre caractère pour coder
*-----------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define E 0X82
int encode_character(int ch,int val);
int decode_character(int ch,int val);
int main (int argc,char *argv[]);
int main (int argc,char *argv[])
{
FILE *fh; /* Descripteur de fichier */
auto int rv =1; /* valeur renvoyée */
auto int ch =0; /* variable pour stocker un caractère */
auto unsigned int ctr =0; /* compteur */
auto int val =5; /* valeur pour coder avec */
auto char buffer[257]; /* le buffer */
if (argc != 3)
{
printf("\nErreur: nombre de parametres incorrecte ...");
printf("\n\nSyntaxe :\n %s nomfichiers action",argv[0]);
printf("\n\n où :");
printf("\n nomfichier = nomdu fichier à coder ");
printf("ou a d%ccoder",E);
printf("\n action = D pour décoder ou C pour coder\n\n");
rv =-1;
}
else
if (( argv[0] == "D") || (argv [0] == "d" )) /* decodage */
{ fh = fopen(argv[1],"r"); /* ouverture du fichier */
if (fh <= 0) /* contrôle */
{
printf("\n\nErreur d'ouverture du fichier ...");
rv = -2; /* valeur de l'erreur renvoyée */
}
else
{
ch = getc(fh); /* lecture d'un caractère */
while ( !feof (fh) ) /* Fin du fichier? */
{
ch = decode_character(ch,val);
putchar(ch); /* affichage du caractère */
ch = getc(fh);
}
fclose(fh);
printf("\n\nFichier d%ccod%c et affich%c",E,E,E);
}
}
else /* Codage dans un fichier */
{
fh = fopen(argv[1],"w");
if(fh <= 0)
{
printf("\n\nErreur pendant la cr%cation du fichier ...",E);
rv =-3; /* valeur renvoyée */
}
else
{
printf("\n\nEntrer le text à coder.");
printf("Entrer une ligne vide pour terminer.\n\n");
while (gets(buffer) != NULL)
{
if(buffer[0] == 0)
break;
for(ctr =0;ctr < strlen(buffer);ctr++)
{
ch = encode_character(buffer[ctr],val);
ch = fputc(ch,fh); /* ecriture du fichier */
}
}
printf("\n\nMessage cod%c et enregistr%c.\n",E,E);
fclose(fh);
}
}
return (rv);
}
int encode_character(int ch,int val)
{
ch += val;
return (ch);
}
int decode_character(int ch,int val)
{
ch -= val;
return (ch);
} |
Partager