[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Split a binary file by searching for string

Posted on 1997-09-04
4
Medium Priority
?
205 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
[X]
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
  • 4
4 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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