So, i'm trying to copy data and add 32kb of 00 between every 32kb (don't ask why)
However, the data gets corrupted after the first 32kb, so the first 32kb from the input file get copied correctly to the output, but the next 32kb get all corrupted (except for a few bytes at the start...)
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
|
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
typedef unsigned int word;
typedef unsigned long int dword;
const word bank (32768);
const word mb (1048576);
char * empty_bank;
char * input;
dword address (0);
///Fill
template<class _copybytes>
_copybytes fill(int _start, int _end, const int _value)
{
for (; _start != _end; ++_start)
{
*_start (_value);
}
}
//Main
int main(int argc, char* argv[]) //_TCHAR*
{
//open files
ifstream rom (argv[1], ios::ate | ios::binary);
ofstream out ("out.put", ios::binary);
//allocate memory
empty_bank = new char [bank];
input = new char [bank];
//fill them with 00
fill(empty_bank + 0, empty_bank + bank, 0);
fill(input + 0, input + bank, 0);
//get size of file
dword size = rom.tellg();
//go to beginning
rom.seekg(address, ios::beg);
//loop!
while (address != size * 2)
{
//go there and get stuff =P
rom.seekg(address);
//read 32kb
rom.read(input, bank);
//go to that there and put stuff over there
out.seekp(address);
//write an empty bank
out.write(empty_bank, bank);
//write input bank
out.write(input, bank);
//increment get position
address = address+bank+bank;
}
//release memory and close the files.
delete[] empty_bank, input;
out.close();
rom.close();
int code = 0;
}
return code;
}
|
So, it copies 32kb of 00, then 32kb of original file, then 32kb of 00, then the next 32kb of the original file, and so on. The first 32kb copy correctly, but the next don't. And also, is there any way of using NULL or something like that in write instead of having to fill an array with 00? And if I try using rom.is_open() and I don't specify anything in the command line, it anyway returns true. So if I don't specify anything, it will never stop copying 00...
And why can't I get rid of stdafx.h?
It's my first program (why do a Hello World if you can look at its code?) so probably many things are done totally wrong, but please, HELP!!!!