?
Solved

Read binary files.

Posted on 2000-03-16
6
Medium Priority
?
284 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 200 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
Independent Software Vendors: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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

764 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