Thread: Error with binary IO

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Question 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.

    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;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Change "reinterpret_cast<char*>(byte)" to "&byte".

    Copy-paste error on line 29.

    gg

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Unrelated to the problem, but instead of having variables drive1, drive2, etc have an array of some type.

    And avoid use of magic numbers in code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    write requires a pointer to the data to write and then number of bytes to write. You did not give it a pointer, but rather a byte value. Hence, the error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    Thank you for the advice. It is now working properly. I have one more question though. With fstream, is the read file location and the write file location the same?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to binary - error
    By randomlock in forum C Programming
    Replies: 2
    Last Post: 08-24-2014, 01:27 PM
  2. Error in binary tree
    By aama100 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2008, 09:11 PM
  3. Error: invalid operand to binary %
    By lilrayray in forum C Programming
    Replies: 16
    Last Post: 07-30-2006, 09:06 AM
  4. error C2678: binary '<'
    By Chike in forum C++ Programming
    Replies: 4
    Last Post: 10-22-2005, 08:30 PM
  5. Help with Error - Binary to Character Converter
    By dvldrmmr in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 01:21 PM

Tags for this Thread