We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Split a binary file by searching for string

iwatkins asked
Medium Priority
Last Modified: 2010-04-04
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.
(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 ?
Watch Question

Hi itwatkins,

Maybe this helps, haven't tested it, so you have to test it yourself. Question, you constantly speak about chars. Is the file a char file? If so, you have to change to code a little bit.

How to call the function :

NewPos := SearchTextNext({File},{Start position},{Str})

{Start Position} = Position in file from where function has to start searching for {Str}

The value in NewPos gives the position of the first byte after {Str}

Type ByteFile : File of Byte;

function SearchTextNext(f:ByteFile;Pos:Integer;Str:ShortString):Integer
var J, OldPos : integer;
    Found : Boolean;
    I : Byte;
 Found := False;
 J := 1;
 OldPos := FilePos(f);
 while not(EOF(f)) and Found then begin
  if (Char(I) = Str[J]) and (OldPos=FilePos(f)-SizeOf(I)) then J := J+1
   else J := 1;
  OldPos := FilePos(f);
  if J = Length(Str)+1 then
    Found = True;
    Result := FilePos(f);

Hope this is what you want.
Have fun,
c.u. ZifNab;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

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;

  AssignFile(OldFile, {FileName});
  FileMode := 0;  ( Set file access to read only }
  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})
           for i := BeginPos To EndPos do begin
     if IORESULT <> 0 then ..... {OOOPS, ERROR}
     Seek(OldFile, EndPos);

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;

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.