1

Voila j'ai écrit un petit programme : // C Source File // Created 09/04/2004; 11:23:37 #define USE_TI89              // Compile for TI-89 #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization #define SAVE_SCREEN           // Save/Restore LCD Contents #include <tigcclib.h>         // Include All Header Files /*********************************************************** *                Prototypes des fonctions                                          * ***********************************************************/ long TailleFichier(char* Nom);                /* Renvoi la taille en                                                                                                octets d'un fichier */                                                                                                 void Crypte(char* Entree, char* Sortie);                                                                                                               /* Crypte le                                                                                                                fichier Entree                                                                                                                et le met dans                                                                                                               sortie */                                                                                                           void Decrypte(char* Entree, char* Sortie);                                                                                                               /* Décrypte le                                                                                                                fichier Entree                                                                                                                et le met dans                                                                                                               sortie */ int CrypteCara(int Car, int Num);                                                                                                /* Crypte un caractère*/                                                                                                 int DecrypteCara(int Car, int Num);                                                                                                                                                                                     /* Décrypte un caractère*/ /*********************************************************** *            Fonction de dépard du programme               * ***********************************************************/ void _main() {      char* nom;      nom = "exemple";      long Taille = 0;      clrscr();            Taille = TailleFichier(nom);      if (Taille > 0)      {           printf("Taille du fichier %s : %ld\n", nom, Taille);                 Crypte("exemple", "sortie");           printf("Appuyez sur une touche pour continuer...");           ngetchx();           Decrypte("sortie", "sortie2");      }      else      {           printf("Erreur !\n");      }      ngetchx();      clrscr(); } /*********************************************************** *              Implémentation des fonctions                * ***********************************************************/ long TailleFichier(char* Nom) {      FILE* fp;      long taille = 0;      fp = fopen(Nom, "rb");      if (fp != NULL)      {           fseek(fp, 0, SEEK_END);           taille = ftell(fp);           fclose(fp);           return taille;      }      else      {           return -1;      } } /**********************************************************/ void Crypte(char* Entree, char* Sortie) {      FILE* fpin;      FILE* fpout;      int taille = TailleFichier(Entree);      int x = 0;      int buff = 0;            fpin = fopen(Entree, "rb");      fpout = fopen(Sortie, "wb");            for(x = 0; x < 2; x++)      {           buff = fgetc(fpin);           fputc(buff, fpout);      }            if((fpin != NULL) && (fpout != NULL))      {           for(x = 0; x < (taille + 1); x++)           {                buff = fgetc(fpin);                printf("%d : %d : ", x, buff);                           buff = CrypteCara(buff, x);                printf("%d\n", buff);                fputc(buff, fpout);           }      }            fputc(TEXT_TAG, fpout);      fclose(fpin);      fclose(fpout); } void Decrypte(char* Entree, char* Sortie) {      FILE* fpin;      FILE* fpout;      int taille = TailleFichier(Entree);      int x = 0;      int buff = 0;            fpin = fopen(Entree, "rb");      fpout = fopen(Sortie, "wb");            for(x = 0; x < 2; x++)      {           buff = fgetc(fpin);           fputc(buff, fpout);      }            if((fpin != NULL) && (fpout != NULL))      {           for(x = 0; x < (taille + 1); x++)           {                buff = fgetc(fpin);                printf("%d : %d : ", x, buff);                buff = DecrypteCara(buff, x);                printf("%d\n", buff);                fputc(buff, fpout);           }      }            fputc(TEXT_TAG, fpout);      fclose(fpin);      fclose(fpout); } int CrypteCara(int Car, int Num) {      Car ^= (Num % 11);      Car += (Num % 11);      Car += ((~Num) % 11);      return Car; } int DecrypteCara(int Car, int Num) {      Car -= ((~Num )% 11);      Car -= (Num % 11);      Car ^= (Num % 11);      return Car; }      

Bon il ne sert pas a gros chose mais j'ai un problème.
La taille de mon fichier de sortie est différente de celui d'origine.
Est ce que quelqu'un voit le probleme ?
Merci d'avance.

2

Ta fonction TailleFichier n'est pas bonne : si tu as

fp=fopen(file,"r");
getc(fp);

alors ftell(fp) n'est pas forcément égal à 1 : les valeurs renvoyées par ftell/prises par fseek sont des trucs internes qui ne correspondent pas nécessairement au nb de caractères lus.

(par exemple, sur TI, c'est le nb d'octets de la représentations interne : "\n" est représenté comme "\r ", qui prend 2 octets)

« The biggest civil liberty of all is not to be killed by a terrorist. » (Geoff Hoon, ministre des transports anglais)

3

pencil
- The value returned from ftell on a text stream has no predictable relationship to the number of characters you have read so far. The only thing you can rely on is that you can use it subsequently as the offset argument to fseek or fseeko to move back to the same file position.
- In a call to fseek or fseeko on a text stream, either the offset must be zero, or whence must be SEEK_SET and and the offset must be the result of an earlier call to ftell on the same stream.

Manuel de glibc, section 12.19 (Portable File-Position Functions)
http://docs.linux.cz/glibc-manual/libc_12.html#SEC223

4

alors comment faut t'il faire pour avoir le nombre d'octets dans le fichier (je voudrai le parcourir ds une boucle for) ?

5

Ben tu comptes le nb d'octets lus avec fgetc, ou tu te débrouilles autrement (en testant avec feof() par exemple, ou encore en regardant si le résultat de fgetc() est négatif).

« The biggest civil liberty of all is not to be killed by a terrorist. » (Geoff Hoon, ministre des transports anglais)

6

Je suis donc obligé de parcourir le fichier en entier pour avoir sa taille exacte ?

7

C'est j'ai trouvé une solution merci de votre aide.