Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- void riempiOrigine(char* nomeFile);
- void determinaNomiFile(int argc,char* argv[], char *s, char *d);
- void leggiChar(char* nomeFile);
- void cripta(char* nomeFile, int* v);
- void visualizzaInt(int* v);
- void scriviFileBin(int* v, char* nomeFile);
- void visualizzaDes(char* nomeFile);
- int main(int argc, char* argv[]){
- srand(time(NULL));
- char sorgente[20];
- char destinazione[20];
- int charCriptati[7];
- for (int i=0; i<7; i++)
- charCriptati[i]=0;
- determinaNomiFile(argc, argv, sorgente, destinazione);
- riempiOrigine(sorgente);
- leggiChar(sorgente);
- cripta(sorgente,charCriptati);
- visualizzaInt(charCriptati);
- scriviFileBin(charCriptati, destinazione);
- visualizzaDes(destinazione);
- system("PAUSE");
- return 0;
- }
- void determinaNomiFile(int argc,char* argv[], char *s, char *d){
- s=argv[1];
- d=argv[2];
- cout<<"Sorgente: "<<s<<"\nDestinazione: "<<d<<endl;
- return;
- }
- void riempiOrigine(char* nomeFile){
- FILE* pf;
- pf=fopen(nomeFile, "wt");
- if (pf!=NULL){
- for (int i=0; i<32+(rand()%40); i++){ //scrive un numero di caratteri compreso tra 32 e 71
- fprintf(pf,"%c",30+rand()%96);
- }
- fclose(pf);
- }
- }
- void leggiChar(char* nomeFile){
- FILE* pf;
- char t;
- pf=fopen(nomeFile, "rt");
- if (pf!=NULL){
- while (!feof(pf)){
- int i=0;
- while (i<32 && !feof(pf)){
- fscanf(pf,"%c",&t);
- cout<<t;
- i++;
- }
- cout<<endl;
- }
- fclose(pf);
- }
- return;
- }
- void cripta(char* nomeFile, int* v){
- FILE* pf;
- char vc[32];
- pf=fopen(nomeFile, "rt");
- if (pf!=NULL){ //legge 32 char dal file di origine
- for (int i=0; i<32; i++)
- fscanf(pf, "%c", &vc[i]);
- fclose(pf);
- }
- for (int i=0; i<32; i++){ //cripta i dati
- for (int j=6; j>=0; j--){
- v[j]=v[j]|(vc[i]%2);
- v[j]=v[j]<<1;
- vc[i]=vc[i]>>1;
- }
- }
- return;
- }
- void visualizzaInt(int* v){
- for (int i=0; i<7; i++){
- cout<<v[i]<<endl;
- }
- return;
- }
- void scriviFileBin(int* v, char* nomeFile){
- FILE* pf;
- pf=fopen(nomeFile,"wb");
- if (pf!=NULL){
- fwrite(v, sizeof (int),7,pf);
- fclose(pf);
- }
- return;
- }
- void visualizzaDes(char* nomeFile){
- FILE* pf;
- int v[7];
- pf=fopen(nomeFile, "rb");
- if (pf!=NULL){
- fread(v, sizeof(int), 7, pf);
- fclose(pf);
- visualizzaInt(v);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement