Solved

Read binary files.

Posted on 2000-03-16
6
278 Views
Last Modified: 2010-04-04
How to:
I want to read the first 1024 bytes of a BINARY file into a STRING variable, after some manipulations, read the next 1024 bytes, some manipulations, another 1024 bytes......at the end of the file, read all the remaining bytes even if it's smaller than 1024 bytes. (I have to know this length)
Please provide me the fastest way...
(D2, examples expected)
0
Comment
Question by:yingkit
6 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2623512
Hi,

use a stream for this. Like:

var
  S: String;
  Bytes: Integer;

begin
  SetLength(S, 1024);
  with TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone) do
  try
    while not Eof do
    begin
      Bytes := Read(PChar(S)^, 1024);
      // Bytes contains now the amount of data read
      // now do something with the data
    end;
  finally
    Free;
  end;
end;

Ciao, Mike
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 2623598
try this:

var
   MyFile: file;
   MyString: String;
   Buf: array[1..1024] of Char;
   i, NumRead: Integer;

begin
   Assign(MyFile, 'MYFILE.DAT');
   Reset(MyFile, 1);
   repeat
      BlockRead(MyFile, Buf, SizeOf(Buf), NumRead);
      SetLength(MyString, NumRead);

      // copy everything into the string
      Move(Buf[0], MyString, NumRead);

      // haven't really tested it the above, but if
      // it doesn't work, then comment the above and uncomment these:

      // for i := 1 to NumRead do
      //     MyString[i] := Buf[i];


      // then, any processing will be done here

   until (NumRead = 0) or (NumWritten <> NumRead);
end.
0
 

Expert Comment

by:aacrg
ID: 2624472
Modification of Lischke's answer: (If its your solution give the points to him!)

var
  S: String;
  Bytes: Integer;

begin
  with TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone) do
  SetLength(S, Size);
  try
    Bytes := Read(PChar(S)^, Size);
    // now do something with the data
  finally
    Free;
  end;
end;
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 10

Expert Comment

by:Lischke
ID: 2625039
aarcg, what sense makes your modification? First of all the code will not compile because you have changed the order of the lines so that the "with" statement will only apply to SetLength(...) not to the try .. finally block. Secondly, you removed the loop. What about files larger than 1024 byte? The point is not to read all at once but 1024 byte per run...

Ciao, Mike
0
 

Author Comment

by:yingkit
ID: 2626462
Hi Mike,
....
while not Eof do
....
I just got an "I/O error 104" in the above line.  Of course, in the following line, the "FileName" is replaced before compilation
with TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone) do
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2627045
Ooooh, I'm so stupid. It must be:

while Position < Size do

Sorry, Mike
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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