Solved

Read binary files.

Posted on 2000-03-16
6
274 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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.

762 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

18 Experts available now in Live!

Get 1:1 Help Now