TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: MICHAEL HEID
from: CHRIS DOWNS
date: 1997-08-02 11:23:00
subject: Re: Editing SEG/OFF

 MH> I am VERY new to C++ and am learing about editing files... anyways, I
 MH> was wondering if their was a way (when using: ofstring
 MH> output("c:\xx");) to edit hexidecimal offsets directly?
 MH> For example:
 MH> 00000000 B803 6E03 FFFF 0000 0000 A06A E140 1300
 MH> 00000010 0000 C066 E140
 MH> I have tried cutting and pasing a line with the 00 hex character into
 MH> a file... but the result was a blank file..
 MH> For example:
 MH> #include 
 MH> #include 
 MH> void main()
 MH> {
 MH> ofstream output("wdial.001")
 MH> output << "..n........j.@.......f.@";
 MH> }
 MH> When I run it and it patches the wdial.001 file,
 MH> it leaves the wdial.001 file blank?? But when 
 MH> even replacing the hex 00 when 01, it works!!?
 First off, you need to specify some more parameters to your ofstream
 constructor call.  The second (optional) parameter specifies the file
 mode.  If you want to "patch" the file, you need to open it for read/
 write access.  Using the default mode (which is the mode chosen when
 you don't specify a 2nd parameter) opens the stream for output only.
 If the file exists, it gets truncated to length zero.
 You'll also need to add the ios::binary flag to prevent automatic
 conversion of the new line indicator.
 So change the constructor call to:
 ofstream output("wdial.001", ios::int | ios::out | ios::binary);
 The next thing you have to do is specify where you want the new data
 written.  When you open the file, the file position pointer is
 pointing at the beginning of the file.  That's where new data will
 be written.  Let's say that you want to patch the 2nd byte of the
 file.  You must now position the file pointer there:
 f.seekp(1, ios::beg);
 Now you're ready to write stuff to the file.
 If you want to use the << operator to write hex to the file, you can
 do something like:
 f << "\x44\x45\0";  // writes an ASCII D, then E then NUL to file
 Note the need for the \x bit before the hex numbers.  Otherwise, you'll
 just get the ASCII digits since the << operator is overloaded for
 text, not binary data.
 One other thing.  This code could fail anywhere along the line for any
 number of reasons (like 'wdial.001' is a read only file) and you
 would never know.  So it's "a good thing" to add error detection to the
 code.  After your I/O calls you can use the overloaded ! operator to
 see if you had success.  For example:
 ofstream f("wdial.001", ios::in | ios::out | ios::binary);
 if (!f)
 {
   cout << "file not opened";
   ErrorReturn();
 }
---
 þ Blue Wave/QWK v2.12 þ
---------------
* Origin: St. Louis Users Group (1:100/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.