Thread: Need some advice on binary editing

  1. #1
    Registered User
    Join Date
    Oct 2011

    Need some advice on binary editing

    Hi there. I've got a problem which I have been stuck for a good whole week already. Tried alot of different methods but to no avail. I'm still new at this, hopefully someone can help me with this? I've got a binary file which I have created in a previous program. So this second program I'm working on is suppose to allow me to edit the binary file which I created. Struct is still the same, I'm still able to open and read the binary file too. Now the problem is that I cant seem to edit and save the updates back into the file. Is there something I'm doing wrong?

    This is just part of the program, in this part I want to increase the variable "size" by 1. size DOES increase by 1, but when I open the binary file again, it goes back to its default value
    void appendStaff (fstream& afile, char nameDat[])
        ABC staff[MAX];
        int size;
        (nameDat, ios::in | ios::out | ios::app | ios::binary); (reinterpret_cast <char *> (&staff), sizeof (staff)); (reinterpret_cast <char *> (&size), sizeof (size));
        size = size + 1;
        afile.write (reinterpret_cast < const char *> (&size), sizeof (staff));
        cout << size <<endl;

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You're giving it the address of size and then telling it to write sizeof(staff) bytes. That sure as heck isn't going to work, in fact I expect it to be somewhat likely to crash!
    You also appear to be writing the new size after the rest of the data. Perhaps you meant to seek back to the start before writing?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2011
    Ahh.. I messed up on that part. I copied from an older version. yes, it is suppose to be sizeof(size).
    I'm a little confused still. So you're saying that I should use a seekp and bring it back to the beginning before I do a write?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Yes, you will need to seek to the position where size is stored in the file before trying to (over)write it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hex editing in C++
    By Gaming in forum C++ Programming
    Replies: 10
    Last Post: 05-01-2008, 08:53 PM
  2. The Best Language For Editing At the Binary level?
    By Kleid-0 in forum A Brief History of
    Replies: 4
    Last Post: 08-31-2005, 04:13 PM
  3. Binary Search Tree- Need Advice
    By Iamien in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2005, 08:18 PM
  4. Some reg editing help please...
    By Rune Hunter in forum Windows Programming
    Replies: 4
    Last Post: 08-22-2004, 05:56 PM
  5. ram editing i think
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 08-18-2002, 11:47 PM