Thread: reading and writing to the end of a binary file

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    reading and writing to the end of a binary file

    Hi all,

    I want to be able to append the raw data value '1' to the end of the exe file "c:/test.exe". Then i want the test.exe file when it is run to output the following message "hello world, the value is 1." If the raw data value was then edited to '2' the test.exe file when ran would say "hello world, the value is 2".

    This is the coding for the editing program so far (this isnt the test.exe code).

    Code:
     fstream binary_file2("c:/test.exe",ios::binary|ios::in);
     int length;
     binary_file2.seekg (0, ios::end);
     length = binary_file2.tellg();
     binary_file2.seekg (0, ios::beg);
     char * FileBuf = new char[length];
     binary_file2.read (FileBuf, length);
     length += 4;   //this is to accomodate for the below line
     int temp = 1;
     FileBuf = (char *)realloc( FileBuf, length );
     memcpy(FileBuf, reinterpret_char<char *>(temp), 4);
     binary_file2.write(FileBuf, length);
     binary_file2.close();
    It compiles fine, but when ran it crashes. Whats wrong? Also how would i go about coding the test.exe file so it reads the the value 1 from the end of itself?

    Thanks all

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Mucking about with an EXE isn't something to be done lightly. You don't just start appending data to the end of an EXE and expect things to work. It might work if you knew the address of something in the data segment of your test.exe program and this other program you were writing could write data to that address. That said, it would be far easier to put the value into a file that your program then reads from. I would strongly suggest doing that instead.

    I would NOT do things the way you are trying to do but I will point out some thigns I noticed with your existing code.

    Code:
     fstream binary_file2("c:/test.exe",ios::binary|ios::in);
     int length;
     binary_file2.seekg (0, ios::end);
     length = binary_file2.tellg();
     binary_file2.seekg (0, ios::beg);
     char * FileBuf = new char[length];
     binary_file2.read (FileBuf, length);
     length += 4;   //this is to accomodate for the below line
     int temp = 1;
     FileBuf = (char *)realloc( FileBuf, length );
     memcpy(FileBuf, reinterpret_char<char *>(temp), 4);
     binary_file2.write(FileBuf, length);
     binary_file2.close();
    First off, you are only opening the file for reading, not read/write. You would also need to or (|) ios::out for that to work. Second, writing to FileBuf is writing to the beginning of the file, not appending to the end like you said you wanted. You would need to write to FileBuf + (original length of the file (before you added the 4)). Third, the cast is wrong, you are attempting to cast the value '1' to a character pointer. What you really want is to cast the address of the variable temp to a character pointer, i.e. reinterpret_cast<char*>(&temp).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks a lot for the reply :-)

    I think i will look into editing the address space of the data segment of the program. How would i go about doing this? I would much rather learn to do it than you just give me the coding, so if you know of a tutroial that would cover this or just give me rough bullet points on how to do this then id be very gratefull

    Thanks for your help mate

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Mucking about with an EXE isn't something to be done lightly
    Right!

    It's much better to use cout << "Hello world. The value is " << x ;

    An EXE file is machine code. It's a just bunch of numbers that represent CPU instructions, number values, addresses, and other data.

    The whole point of using C++ is so that you don't have to write machine language (or assembly language)! You might be surprised at the amount of machine code required for "Hello World."

    If you want to "look at" the machine-code you can use a hex editor or a disassembler. Your compiler may include these tools, or you can find 'em free on the net. (I have a free hex ediitor on my machine at home... but I can't remember it's name.)

    If you use a hex editor to search through the EXE file, you will find your Hello Word string. Note that the ASCII character 1 will be represented by the hex value 31 (decimal 49). You can change that '1' to a '2' (32 hex). You could write a program to do this too, but it's a really BAD idea!

    Sometimes "hackers" will use a hex editor to change a program when they don't have the source code. And, sometimes a hex editor is used to "patch" a program.

    Most hex editors will display the ASCII characters for all values that fall into the ASCII character-range, so you shouldn't have too much trouble finding the "Hello World" string. (The hex editor doesn't know if 31 hex represents a '1', the number 49, or a machine instruction... Wherever there's a 31 hex, it will display a '1' in the ASCII field.)

    While we're on the subject, assembly language is a human-readable representation of machine code. (Lots of people program in assembly, almost nobody programs in pure machine code.)
    Last edited by DougDbug; 06-06-2005 at 04:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM