943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1136
  • C++ RSS
Feb 25th, 2009
0

Problem with writing hex values

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
replic is offline Offline
20 posts
since Nov 2008
Feb 25th, 2009
0

Re: Problem with writing hex values

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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Feb 27th, 2009
0

Re: Problem with writing hex values

It still doesn't work using this code.
It produces the same error again.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 2
Newbie Poster
replic is offline Offline
20 posts
since Nov 2008
Feb 27th, 2009
1

Re: Problem with writing hex values

To get 'non-destructive writes', open the file specifying also the ios::in mode.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 1st, 2009
0

Re: Problem with writing hex values

Yes that did the trick thanks a lot.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
replic is offline Offline
20 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Standard Output Error: Forking to Obtain Prime Numbers
Next Thread in C++ Forum Timeline: Need help in a program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC