Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15760

Re: on importing/exporting binary files

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: on importing/exporting binary files
Date 2011-02-12 10:22 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ij5n09$joo$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 2/11/11 at 4:17 AM, l.balzano@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"];

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: on importing/exporting binary files Bill Rowe <readnews@sbcglobal.net> - 2011-02-12 10:22 +0000

csiph-web