Solved

Read binary files.

Posted on 2000-03-16
6
281 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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