I am trying to make a program like a virtual machine, and therefore I need to have a virtual hard drive. I have it split across four files, each being exactly 67,108,864 bytes (at this point, the files consist of 0x00 throughout the entire file). When I try to write to the beginning of one of the files, I get a "EXC_BAD_ACCESS (code=1, address=0xff)" from Xcode. I have no idea what I am doing wrong. Here is my source code.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream drive1("main1.vhd", ios::in | ios::out | ios::binary);
fstream drive2("main2.vhd", ios::in | ios::out | ios::binary);
fstream drive3("main3.vhd", ios::in | ios::out | ios::binary);
fstream drive4("main4.vhd", ios::in | ios::out | ios::binary);
drive1.seekg(0, ios::end);
const long size1 = drive1.tellg();
drive1.seekg(0, ios::beg);
drive2.seekg(0, ios::end);
const long size2 = drive2.tellg();
drive2.seekg(0, ios::beg);
drive3.seekg(0, ios::end);
const long size3 = drive3.tellg();
drive3.seekg(0, ios::beg);
drive4.seekg(0, ios::end);
const long size4 = drive4.tellg();
drive2.seekg(0, ios::beg);
if (size1!=67108864 && size2!=67108864 && size3!=67108864 && size4!=67108864)
{
cout << "The hard drive files are not the correct size and cannot be addressed properly\n";
return 9;
}
drive1.seekp(0, ios::beg);
drive2.seekp(0, ios::beg);
drive3.seekp(0, ios::beg);
drive4.seekp(0, ios::beg);
unsigned char byte = 0xff;
drive1.write(reinterpret_cast<char*>(byte), sizeof(byte));
return 0;
}