Writting hex value into files

This is a discussion on Writting hex value into files within the C++ Programming forums, part of the General Programming Boards category; FWIW, this is the results of my tinkering. I think we're at wits end and you need to see, realistically, ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    FWIW, this is the results of my tinkering. I think we're at wits end and you need to see, realistically, what you are not doing.

    Code:
    #include <fstream>	// for file api
    #include <vector>	// is string type
    #include <iostream>	// for cout, cin, endl
    #include <iomanip>	// for hex manipulator
    #include <cstdlib>	// for rand ()
    
    #define BYTES 16
    
    int main ()
    {
    	using namespace std;
    
    	fstream dat;
    	// generating data for proof-of-concept
    	dat.open ("dump.dat", ios::out | ios::binary);
    	if (dat.is_open()) 
    	{
    		char byte;
    		for (int i = 0; i < BYTES; i++) 
    		{
    			byte = (rand () % 99) & 0xff;
    			cout << "writing value 0x" << hex << int(byte) << endl;
    			dat.write (&byte, sizeof byte);
    			if (!dat)
    			{
    				cout << "there was a problem writing the file" << endl;
    				return 0;
    			}
    		}
    		dat.close ();
    	}
    	else 
    	{
    		cout << "buy a bigger hard drive" << endl;
    		return 0;
    	}
    
    	// prove we can read what we wrote
    	vector< char > string (BYTES);
    	dat.open ("dump.dat", ios::in | ios::binary);
    	if (dat.is_open())
    	{
    		dat.read (&string[0], BYTES);
    		if (dat)
    		{
    			for (int i = 0; i < string.size (); i++) {
    				cout << "read value 0x" << hex << int(string[i]) << endl;
    			}
    			dat.close ();
    		}
    		else {
    			cout << "problem reading the file" << endl;
    			return 0;
    		}
    	}
    	
    	// now we edit it
    	vector< char >::size_type offset = 0;
    			
    	cout << "enter the offset:" << endl;
    	cin >> offset;
    	// below statement refers to the exact byte,
    	// while protecting the bounds of string.
    	char &byte = string.at (offset);
    			
    	cout << "enter the byte's new value:" << endl;
    	cin >> hex >> byte;
    
    	// finalize the edit
    	dat.open ("dump.dat", ios::out | ios::binary);
    	if (dat.is_open())
    	{
    		dat.write (&string[0], string.size ());
    		if (dat)
    		{
    			for (int i = 0; i < string.size (); i++) {
    				cout << "wrote value 0x" << hex << int(string[i]) << endl;
    			}
    			string.clear ();
    			dat.close ();
    		}
    		else {
    			cout << "problem rewriting the file" << endl;
    			return 0;
    		}
    	}
    	
    	system ("pause");
    
    	// inspect your file -- it works!
    	return 0;
    }
    The example has challenges. Chiefly, because I don't know how to tell my present compiler to treat char as unsigned char, I definitely couldn't tell you how to do that with your software even if we both had the same compiler. So the output may look different, depending on the inputs, because of char being signed. If the appropriate switch were turned on, then the fact that this works would be much more apparent. I apologize for my ignorance. I also don't buy that you are just editing random bytes, and that there is a decent chance your file is record based, but I went and showed that you can indeed edit random bytes. If you were doing this with a record based file, then you would read the raw input into an instance of the very class that you saved instead of a string. The bit-fiddling part would have to be done on the class members and then re-saved.

    Unless the file is really big, don't bother seeking it.
    Last edited by whiteflags; 11-03-2010 at 11:49 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21