Problem with writing hex values

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 8
Reputation: replic is an unknown quantity at this point 
Solved Threads: 0
replic replic is offline Offline
Newbie Poster

Problem with writing hex values

 
0
  #1
Feb 25th, 2009
Hi everyone,
i wrote this small app and it works fine but it erases everything up to the offset and everything after it.
Anyone knows how to avoid that?

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main()
  5. {
  6. int hex = 0x75;
  7. std::ofstream f("sample.exe", std::ios::binary);
  8. f.seekp(1169, std::ios::beg);
  9. f.write(reinterpret_cast<const char *>(&hex), 2);
  10. f.close();
  11. return 0;
  12. }

regards
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Problem with writing hex values

 
0
  #2
Feb 25th, 2009
also use mode std::ios::ate (AT End)

The default mode for ofstream opening is std::ios::trunc, (TRUNCate), which deletes any previous content of the file. ios::ate preserves the file, allows you to move the file pointer anywhere within the current content.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: replic is an unknown quantity at this point 
Solved Threads: 0
replic replic is offline Offline
Newbie Poster

Re: Problem with writing hex values

 
0
  #3
Feb 27th, 2009
It still doesn't work using this code.
It produces the same error again.

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main()
  5. {
  6. int hex = 0x75;
  7. std::ofstream f("sample.exe", std::ios::binary | std::ios::ate);
  8. f.seekp(1169, std::ios::beg);
  9. f.write(reinterpret_cast<const char *>(&hex), 2);
  10. f.close();
  11. return 0;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Problem with writing hex values

 
1
  #4
Feb 27th, 2009
To get 'non-destructive writes', open the file specifying also the ios::in mode.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: replic is an unknown quantity at this point 
Solved Threads: 0
replic replic is offline Offline
Newbie Poster

Re: Problem with writing hex values

 
0
  #5
Mar 1st, 2009
Yes that did the trick thanks a lot.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC