08.04.2014 Views

Algorithmique et Langage - Pages de Michel Deloizy - Free

Algorithmique et Langage - Pages de Michel Deloizy - Free

Algorithmique et Langage - Pages de Michel Deloizy - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TP4 : FICHIERS<br />

I. Lecture d'un fichier texte<br />

Éditer (avec un éditeur <strong>de</strong> textes) un texte quelconque <strong>de</strong> quelques lignes.<br />

Enregistrer ce fichier sur le disque.<br />

Créer un programme qui lit ce fichier <strong>et</strong> affiche son contenu. Le programme affichera finalement le nombre <strong>de</strong><br />

lignes composant le fichier.<br />

II. Cryptage d'un fichier<br />

On se propose <strong>de</strong> crypter <strong>de</strong> manière simple le fichier précé<strong>de</strong>mment créer (afin d'en interdire la lecture avec un<br />

simple éditeur <strong>de</strong> textes).<br />

II.1. Faire la fonction <strong>de</strong> cryptage<br />

L'oct<strong>et</strong> transmis sera divisé en <strong>de</strong>ux quart<strong>et</strong>s. Les bits <strong>de</strong>s quart<strong>et</strong>s seront ensuite mélangés comme indiqué ci<strong>de</strong>ssous.<br />

La fonction r<strong>et</strong>ournera le complément du résultat obtenu.<br />

Exemple : Soit le caractère 'A' <strong>de</strong> co<strong>de</strong> ASCII 0x41<br />

On coupe ce co<strong>de</strong> ASCII en <strong>de</strong>ux quart<strong>et</strong>s ; on obtient 4 <strong>et</strong> 1, soit en binaire : 0100 <strong>et</strong> 0001.<br />

On mélange les quart<strong>et</strong>s en alternant les bits :<br />

0 1 0 0<br />

0 0 0 1<br />

0<br />

1 0 0 1 0 0 0<br />

On obtient alors un oct<strong>et</strong> contenant 0x48, soit, en binaire : 01001000<br />

Ensuite on prend le complément <strong>de</strong> c<strong>et</strong>te valeur <strong>et</strong> on obtient 10110111, soit en hexadécimal 0xB7.<br />

Si le caractère 'A' est transmis à la fonction, celle-ci r<strong>et</strong>ournera donc la valeur B7 H (soit 183 en décimal).<br />

II.2. Écrire une fonction réalisant la lecture du fichier Fin, qui crypte ce fichier <strong>et</strong> écrit le résultat dans Fout.<br />

Le prototype <strong>de</strong> c<strong>et</strong>te fonction sera :<br />

int CryptFile(FILE *Fin, FILE *Fout);<br />

C<strong>et</strong>te fonction r<strong>et</strong>ournera 1 si le cryptage s'est bien passé, <strong>et</strong> 0 en cas d'erreur.<br />

Remarque : Le fichier Fout <strong>de</strong>vra avoir été ouvert en binaire pour assurer une écriture correcte dans le fichier.<br />

II.3. Faire le programme compl<strong>et</strong>.<br />

L'utilisateur <strong>de</strong>vra entrer le nom du fichier à crypter <strong>et</strong> le nom du fichier crypté.<br />

II.4. 4 On souhaite appeler le programme <strong>de</strong> cryptage selon la syntaxe suivante :<br />

NomProg NomFichier<br />

où NomProg est le nom du programme exécutable (.exe) <strong>et</strong> NomFichier est le nom du fichier à crypter.<br />

Le fichier transmis sera alors crypté, le résultat étant mis à la place du fichier d'origine.<br />

Faire la lecture <strong>de</strong>s arguments <strong>de</strong> la fonction main(), en sachant que le prototype <strong>de</strong> main() est<br />

int main(int argc, char *argv[]);<br />

où argc représente le nombre d'arguments sur la ligne <strong>de</strong> comman<strong>de</strong> (y compris le nom du programme) <strong>et</strong> argv est<br />

un tableau <strong>de</strong> chaînes <strong>de</strong> caractères contenant les différents arguments.<br />

Écrire le programme compl<strong>et</strong>. On pourra utiliser les fonctions tmpnam() , rename() <strong>et</strong> remove() en s'assurant que le<br />

fichier d'origine ne doit être détruit que si le fichier crypté a été construit sans erreur.<br />

Proposer un système perm<strong>et</strong>tant <strong>de</strong> crypter ou décrypter <strong>de</strong> manière automatique le fichier transmis en argument.<br />

4 Exercice optionnel.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!