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
|
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main()
{
typedef istream_iterator<unsigned char> input_iter_t;
typedef ostream_iterator<unsigned char> output_iter_t;
// First replacement stream
const off_t SIZE_001 = 13;
char search_001[SIZE_001] = { 0x00, 0x2E, 0x2E, 0x2E, 0x00, 0x2E, 0x2E, 0x2E, 0x00, 0x2E, 0x2E, 0x2E, 0x00 };
char replace_001[SIZE_001] = { 0x0D, 0x0A, 0x0D, 0x0A };
// Second replacement stream
const off_t SIZE_002 = 2;
char search_002[SIZE_002] = { 0x00 };
char replace_002[SIZE_002] = { 0x0D, 0x0A };
string filein;
// cout << "Enter the file name:\n"; //Tells user to input a file name
// cin >> filein; //User inputs incoming file name
fstream f("test.exe", ios::binary | ios::in | ios::out);
// fstream f(filein.c_str(), ios::binary | ios::in | ios::out);
while ( !f.eof() ) //read the file again until we reach end of file
{
// First replacement
if (search(input_iter_t(f), input_iter_t(), search_001, search_001 + SIZE_001) != input_iter_t())
{
f.seekp(-SIZE_001, ios::cur);
f.write(replace_001, SIZE_001);
f.seekp(0, ios::beg); // set put pointer to beggining of file
}
}
f.clear();
f.seekg(0, ios::beg); // return to beggining of file
while ( !f.eof() ) //read the file again until we reach end of file
{
// Second replacement
if (search(input_iter_t(f), input_iter_t(), search_002, search_002 + SIZE_002) != input_iter_t())
{
f.seekp(-SIZE_002, ios::cur);
f.write(replace_002, SIZE_002);
f.seekp(0, ios::beg); // set put pointer to beggining of file
}
}
}
|