wtorek, 13 czerwca 2017

Encoding data from a file (linux/g++)

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define path_file "/proc/cpuinfo"
#define new_path_file "cpuinfo_coding"

using namespace std;

void init_Random()
{
    srand(time(NULL));
}
char int_to_Ascii(int x)
{
    return char(x);
}
void double_Code(char *test_file)
{
    int value_ascii,i;
    char character;
    value_ascii=rand()%255;
    character=int_to_Ascii(value_ascii);
    for(i=0;test_file[i]!=NULL;i++)
     test_file[i]=test_file[i]^character;
    for(i=0;test_file[i]!=NULL;i++)
     test_file[i]=255-test_file[i];      
}
int main(int argc, char **argv)
{
 init_Random();   
 char *temporary_bufor;
 temporary_bufor=new char[2048];   
 std::fstream test_file( path_file, std::ios::in );
 test_file.read(temporary_bufor,2048);
 test_file.close();
 cout<<"Before coding:\n";
 cout<<temporary_bufor<<endl;
 double_Code(temporary_bufor);
 cout<<"\n\nAfter coding:\n\n";

 cout<<temporary_bufor<<endl;
 int new_file=open(new_path_file,O_WRONLY | O_CREAT | O_APPEND, 0666);
 size_t length_file=strlen(temporary_bufor);
 write(new_file,temporary_bufor,length_file);
 close(new_file);
 delete temporary_bufor;
 return 0;
}

Brak komentarzy:

Prześlij komentarz