Solved

Split a binary file by searching for string

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How can I open IE in olecontainer? 7 109
Printing problem 2 73
find a node in VST 2 47
drawing animated level bar based on numbers 3 75
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

13 Experts available now in Live!

Get 1:1 Help Now