1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
//// TO GENERATE RANDOM STRING
static const char alphanum[] =
"0123456789"
"!@#$%^&"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = sizeof(alphanum) - 1;
///TO GET THE SIZE OF A FILE
streampos filesize(const char *filename)
{
ifstream f(filename, ios::in|ios::ate);
if(!filename)
cout<<"\nerror required filename not found";
streampos size = f.tellg();
f.close();
return size;
}
////// TO GENERATE RANDOM STRING
char genRandom() // Random string generator function.
{
return alphanum[rand() % stringLength];
}
char randgen()
{
char a;
for(int z=0; z < 21; z++)
{
a=genRandom();
}
return a;
}
// FUNCTION TO CUT THE FILE
void filecutter()
{
srand(time(0));
ofstream log;
log.open("LOGFILE.txt",ios::out);
double n;
char chara[50],i[8],letter;
cout<<"enter file name:";
gets(chara);
double c;
c=filesize(chara);
cout<<"file has "<<c<<" characters\n";
cout<<"enter number of files you want to cut it into:";
cin>>n;
long int no=c/n;
cout<<"\neach file would have "<<no<<" characters\n";
cout<<n<<" files will be created\n";
fstream fc;
fc.open(chara,ios::in|ios::app|ios::binary);
double tell;
fc.seekg(0);
for( int j=0;j<n;++j)
{
for(int t=0;t<8;t++)
{
i[t]=randgen();
}
ofstream fcs;
fcs.open(i,ios::app);
for (long int flag = 0; flag<no;flag++)
{
fc.get(letter);
fcs.put(letter);
letter='\0';
cout<<flag<<" character out of "<<no<<" wrote to "<<i<<"\n";
}
log<<i;
memset(i,'\0',8);
cout<<j<<"/"<<n<<" files created\n";
fcs.close();
}
ofstream fx;
fx.open("EXTRA",ios::app|ios::binary);
while(!fc.eof())
{
fc.get(letter);
fx.put(letter);
}
fc.close();
cout<<"complete";
log.close();
ofstream lsize;
lsize.open("LOGSIZE.txt",ios::out);
lsize<<c<<" "<<no<<" "<<n;
lsize.close();
}
//TO PUT BACK THE FILES FROM THE AVAILABLE PIECES
void filehealer()
{
char ch,fname[8];
double i,n;
double filesize;
long int ofilesize;
ofstream original;
original.open("ORIGINALFILE",ios::out|ios::binary);
ifstream lsize;
lsize.open("LOGSIZE.txt",ios::in);
lsize.seekg(0);
lsize>>filesize;
lsize>>ofilesize;
lsize>>n;
cout<<"original file size :"<<filesize<<"\n";
cout<<"part size:"<<ofilesize<<"\n";
cout<<"no of parts:"<<n<<"\n";
getch();
ifstream log;
log.open("LOGFILE.txt",ios::in);
while(!log)
{
cout<<"\n logfile not found\n";
getch();
exit(0);
}
for(i=0;i<n;++i)
{
log.get(fname,9);
cout<<fname<<"\n";
getch();
ifstream parts;
parts.open(fname,ios::in);
if(!parts)
{
cout<<"file/files not found aborting";
getch();
exit(0);
}
parts.seekg(0);
for(long int j=0;j<ofilesize;j++)
{
parts.get(ch);
original.put(ch);
ch='\0';
cout<<j<<"\nbytes++";
}
memset(fname,'\0',8);
parts.close();
cout<<i<<"/"<<n<<" files created\n";
}
ifstream extra;
extra.open("EXTRA",ios::in);
while(!extra.eof())
{
extra.get(ch);
original.put(ch);
ch='\0';
}
extra.close();
lsize.close();
log.close();
original.close();
cout<<"completed";
}
|