Error with binary IO

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.

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
#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;
}
You are writing to disk, from memory at address 0xff - maybe you meant to write 0xff to disk instead of the memory at 0xff?

drive1.write(&byte, sizeof(byte));

Always remember: if you have to cast, something is wrong.
Last edited on
> Always remember: if you have to cast, something is wrong.
foo.cpp:42:15: error: cannot initialize a parameter of type 'const char_type *' (aka 'const char *') with an rvalue of type 'unsigned char *'
        drive1.write(&byte, sizeof(byte));
                     ^~~~~
Last edited on
I was wrong :)
Topic archived. No new replies allowed.