Solved

Split a binary file by searching for string

Posted on 1997-09-04
4
181 Views
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.
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 ?
0
Comment
Question by:iwatkins
  • 4
4 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1343661
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;
begin
 Found := False;
 J := 1;
 Seek(f,Pos);
 OldPos := FilePos(f);
 while not(EOF(f)) and Found then begin
  Read(f,I);
  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
   begin
    Found = True;
    Result := FilePos(f);
   end;
 end;
end;

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

Expert Comment

by:ZifNab
ID: 1343662
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;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343663
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;

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1343664
So it worked! Great!

Have fun,
c.u. ZifNab;


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now