Solved

Read binary files.

Posted on 2000-03-16
6
276 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

16 Experts available now in Live!

Get 1:1 Help Now