Link to home
Start Free TrialLog in
Avatar of iwatkins
iwatkins

asked on

Split a binary file by searching for string

I have a data file (binary) within which are contained
further data files. i.e.

GRIB.........(unknown number of chars)......GRIB.........
(unknown number of chars)......GRIB.........
(unknown number of chars)......GRIB.........
(unknown number of chars)......GRIB.........
(unknown number of chars)......
etc. etc.
GRIB.........
(unknown number of chars)......EOF

What I want to be able to do, is to read the datafile,
search for the first occurance of the string GRIB then
write GRIB and all the next characters out to file number
one, until GRIB occurs again. At this point, I will write
GRIB and all the characters out to file number two, until
GRIB occurs again of EOF. i.e. I want to split the
datafile into smaller files using the string GRIB as the
cutting point..

Anybody have a code segment (function) to do this ?
ASKER CERTIFIED SOLUTION
Avatar of ZifNab
ZifNab

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ZifNab
ZifNab

Hi iwatkins,

Use function like this :

Euhm, better take backup of your data file!

Haven't tested this.

var OldFile, NewFile   : ByteFile;
    BeginPos, EndPos,I : Integer;
    chars              : Byte;

begin
  {$I-}
  AssignFile(OldFile, {FileName});
  FileMode := 0;  ( Set file access to read only }
  Reset(OldFile);
  {$I+}
  if IORESULT <> 0 then .... {Error}
   else begin
    BeginPos := SearchNextFile(OldFile,FilePos(OldFile),'GRIB');
    while not EOF(OldFile) do begin
     EndPos  := SearchNextFile(OldFile,FilePos(OldFile),'GRIB');
     Seek(OldFile, BeginPos);
  {Make newfile ready, change filename!! Not done in this demo!}
     {$I-} AssignFile(NewFile, {FileName})
           Rewrite(NewFile);
           for i := BeginPos To EndPos do begin
            read(OldFile,Chars);
            write(NewFile,Chars);
           end;
           CloseFile(NewFile);
     {$I+}
     if IORESULT <> 0 then ..... {OOOPS, ERROR}
     Seek(OldFile, EndPos);
    end;  
   end;
  {$I-}
  CloseFile(OldFile);
  {$I+}
end;

that's how you call it in your program. If this is what you needed and you need some answer, please be quick to ask them because I'm going on holiday soon.

Have fun,
In the sun,
c.u. ZifNab;
And offcourse,
a fault!

changes :

EndPos := SearchNextFile(OldFile,BeginPos,'GRIB');

if IORESULT <> 0 then ..... {OOOPS, ERROR}
Seek(OldFile, EndPos);
BeginPos := EndPos;

Have fun,
c.u. ZifNab;

So it worked! Great!

Have fun,
c.u. ZifNab;