Re: on importing/exporting binary files
- To: mathgroup at smc.vnet.net
- Subject: [mg116388] Re: on importing/exporting binary files
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 12 Feb 2011 05:22:11 -0500 (EST)
On 2/11/11 at 4:17 AM, l.balzano at gmail.com (Luigi B) wrote: >I have some files that contain a text header followed by binary data. >My problem is to open the file, change a line in the (text) header >and close it (saving it). I tried with "Import" but it takes long >time and when I "export" the files the binary part gets messed up. >Does anyone know how to do this efficiently as I have thousands of >files that need to be processed? I would do this using the low level functions input/output functions and write my own routine to read in the header and write out the modified header. The sequence would be something like: str=OpenWrite[filename]; SetStreamPosition[str, posOfStartOfLineToChange]; WriteString[str, newText] Close[str]; Note, the above assumes the new text being written to the file is exactly the same length of as the text you are replacing. If this is not the case, you will need to read in the entire file, modify it then write it back out. If the modification changes the line length, I would do something like data=BinaryRead[filename,"Byte"]; modify the bytes I wanted changed then do BinaryWrite[filename, changedData, "Byte"];