Advertisement
filomancio

PROG

Dec 6th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5. void riempiOrigine(char* nomeFile);
  6. void determinaNomiFile(int argc,char* argv[], char *s, char *d);
  7. void leggiChar(char* nomeFile);
  8. void cripta(char* nomeFile, int* v);
  9. void visualizzaInt(int* v);
  10. void scriviFileBin(int* v, char* nomeFile);
  11. void visualizzaDes(char* nomeFile);
  12.  
  13. int main(int argc, char* argv[]){
  14.     srand(time(NULL));
  15.     char sorgente[20];
  16.     char destinazione[20];
  17.     int charCriptati[7];
  18.     for (int i=0; i<7; i++)
  19.         charCriptati[i]=0;
  20.     determinaNomiFile(argc, argv, sorgente, destinazione);
  21.     riempiOrigine(sorgente);    
  22.     leggiChar(sorgente);
  23.     cripta(sorgente,charCriptati);
  24.     visualizzaInt(charCriptati);
  25.     scriviFileBin(charCriptati, destinazione);
  26.     visualizzaDes(destinazione);
  27.     system("PAUSE");
  28.     return 0;
  29. }
  30.  
  31. void determinaNomiFile(int argc,char* argv[], char *s, char *d){
  32.      s=argv[1];
  33.      d=argv[2];
  34.      cout<<"Sorgente: "<<s<<"\nDestinazione: "<<d<<endl;
  35.      return;
  36. }
  37.  
  38. void riempiOrigine(char* nomeFile){
  39.     FILE* pf;
  40.     pf=fopen(nomeFile, "wt");
  41.     if (pf!=NULL){
  42.         for (int i=0; i<32+(rand()%40); i++){           //scrive un numero di caratteri compreso tra 32 e 71
  43.             fprintf(pf,"%c",30+rand()%96);
  44.         }
  45.         fclose(pf);
  46.     }
  47. }
  48.  
  49. void leggiChar(char* nomeFile){
  50.     FILE* pf;
  51.     char t;
  52.     pf=fopen(nomeFile, "rt");
  53.     if (pf!=NULL){
  54.         while (!feof(pf)){
  55.             int i=0;
  56.             while (i<32 && !feof(pf)){
  57.                 fscanf(pf,"%c",&t);
  58.                 cout<<t;
  59.                 i++;
  60.             }
  61.             cout<<endl;
  62.         }
  63.         fclose(pf);
  64.     }
  65.     return;
  66. }
  67.  
  68. void cripta(char* nomeFile, int* v){
  69.     FILE* pf;
  70.     char vc[32];
  71.     pf=fopen(nomeFile, "rt");
  72.     if (pf!=NULL){ //legge 32 char dal file di origine
  73.         for (int i=0; i<32; i++)
  74.             fscanf(pf, "%c", &vc[i]);
  75.         fclose(pf);
  76.     }
  77.     for (int i=0; i<32; i++){ //cripta i dati
  78.         for (int j=6; j>=0; j--){
  79.             v[j]=v[j]|(vc[i]%2);
  80.             v[j]=v[j]<<1;
  81.             vc[i]=vc[i]>>1;
  82.         }
  83.     }
  84.     return;
  85. }
  86.  
  87. void visualizzaInt(int* v){
  88.     for (int i=0; i<7; i++){
  89.         cout<<v[i]<<endl;
  90.     }
  91.     return;
  92. }
  93.  
  94. void scriviFileBin(int* v, char* nomeFile){
  95.     FILE* pf;
  96.     pf=fopen(nomeFile,"wb");
  97.     if (pf!=NULL){
  98.         fwrite(v, sizeof (int),7,pf);
  99.         fclose(pf);
  100.     }
  101.     return;
  102. }
  103.  
  104. void visualizzaDes(char* nomeFile){
  105.     FILE* pf;
  106.     int v[7];
  107.     pf=fopen(nomeFile, "rb");
  108.     if (pf!=NULL){
  109.         fread(v, sizeof(int), 7, pf);
  110.         fclose(pf);
  111.         visualizzaInt(v);
  112.     }
  113.     return;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement