Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Split a binary file by searching for string

Posted on 1997-09-04
4
186 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

829 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