Solved

The Blockread/BlockWrite

Posted on 1998-10-02
13
717 Views
Last Modified: 2010-04-03
The Bockread and BockWrite 2 part question. the first how do I get the value of the data in the buffer, is it a Integer or string I know it ia a bite but how to read the number. (need to do soom fine file manipulation. Second question- The blockread and blockwrite how do I use them for one bite Read/Write. I'v looked in Delphi2 developers guide and studied the example but still got lost on it. Need Examples.

 
0
Comment
Question by:PPaul
13 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341553
PPaul, what exactly do you want to get, because TFileStream is also a way to get good file manipulation. Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341554
FileStream objects let you read and write binary date. They don't know or even care what the data is. they simply let you transfer a stream of bytes between a buffer area in memmory and a disk file. (Delphi informant volume 4, nr. 6)

I think this is better for your needs...

example:

var InStream, OutStream : TFileStream;
    Buff: array[0..31] of Char;
    Count : LongInt;

begin
 {Create the input file stream.}
 InStream := TFileStream.Create('Stream.bin', fmOpenRead);
 {Create the output file stream.}
 OutStream := TFileStream.Create('Stream.Out', fmCreate);
 try
  {Move the stream pointer to the location where you want to begin reading }
 Instream.Seek(10,0);
 {Read some bytes from the stream }
 Count := InStream.Read(Buff, 5);
 {Put a null at the end of the buffer so you can treat it as a null-terminated string (just for this example)}
 Buff[count] :=#0;
 {Display what you have read}
 Label1.Caption := StrPas(Buff);
 {Write contents of buffer to the output stream.}
 OutStream.Write(Buff,Count);
finally
 InStream.Free;
 OutStream.Free;
end;
end;

 {for full example see delphi informant as above mentioned}

More information needed?

If this isn't what you like, please reject this answer.

Regards, Zif.
0
 
LVL 1

Expert Comment

by:titz
ID: 1341555
hi ppaul,

i think here is your solution :

   procedure test;

   CONST
      blocklaenge         = 20768; (* must be lower than 32000 *)

   type
      puffertyp           = ARRAY[1..blocklaenge] OF byte;

   VAR
      c                   : char;
      dname               : string[100];
      fin                 : FILE;
      puffer              : puffertyp;
      r_in, r_out         : word;

   BEGIN
   assignfile(fin, dname);
   reset(fin, 1);
   REPEAT
   blockread(fin, puffer, blocklaenge, r_in);

(*
do something with the puffer
.
you can work with each byte in the PUFFER:
e.g.
   if r_in>0 then
      begin
      for j:=1 to r_in
         begin
         c:=chr(puffer[j]);
         if c=...  then
            ...
         end;
      end;

be carefull : r_in can be between 0 and blocklaenge !!
*)

   UNTIL (r_in = 0) ;
   closefile(fin);
ende:
   END;

Every Byte in the PUFFER you can interpret as char (see above) or as byte.

good work !
titz
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

Expert Comment

by:lgc5800
ID: 1341556
Hello, here is an other example
I have no problem with BLOCKREAD and BLOCKWRITE
on D1,2,3,4
Chance.
Jones.


procedure TSDIAppForm.SpeedButton9Click(Sender: TObject);
var
 bc    : integer;
 DepuisF, DansF:    FILE;
 NumLus, NumEcrits: integer;
 tampon : ARRAY[1..2048] OF CHAR;

begin
if actif = '' then exit;
nname.showmodal;
if nname.modalresult = mrcancel then exit;
nom := nname.edit1.text;
if nom = '' then begin showmessage(' ERROR : ='''); exit; end;
if length(nom) < 2 then begin showmessage(' ERROR : < 2'); exit; end;
if length(actif) > 6 then begin showmessage(' ERROR : > 6'); exit; end;
screen.cursor := crhourglass;


assignfile(depuisF,chemhk804+'\'+actif+nom+'.db');
{$i-}reset(DepuisF);{$i+}
if ioresult = 0 then
                begin
                screen.cursor := crdefault;
                showmessage('???????????? FILE ALREADY EXIST ?????????????');
                closefile(depuisf);
                exit;
                end;

table1.close;

  Assignfile(DepuisF,chemhk804+'\'+actif+'.db');
  {$i-;}reset(DepuisF);{$i+};
   if ioresult <> 0 then exit else closefile(DepuisF);
  Reset(DepuisF, 1);
  Assignfile(DansF,chemhk804+'\'+actif+nom+'.db');
  Rewrite(DansF, 1);
  REPEAT
    BlockRead(DepuisF,Tampon,
              SizeOf(Tampon),NumLus);
    BlockWrite(DansF,Tampon,NumLus,NumEcrits);
{    nb := nb + numecrits;}
  UNTIL (NumLus = 0) OR
        (NumEcrits <> NumLus);
  Closefile(DepuisF);
  Closefile(DansF);

  Assignfile(DepuisF,chemhk804+'\'+actif+'.px');
  {$i-;}reset(DepuisF);{$i+};
   if ioresult <> 0 then exit else closefile(DepuisF);
  Reset(DepuisF, 1);
  Assignfile(DansF,chemhk804+'\'+actif+nom+'.px');
  Rewrite(DansF, 1);
  REPEAT
    BlockRead(DepuisF,Tampon,
              SizeOf(Tampon),NumLus);
    BlockWrite(DansF,Tampon,NumLus,NumEcrits);
{    nb := nb + numecrits;}
  UNTIL (NumLus = 0) OR
        (NumEcrits <> NumLus);
  Closefile(DepuisF);
  Closefile(DansF);

  Assignfile(DepuisF,chemhk804+'\'+actif+'.mb');
  {$i-;}reset(DepuisF);{$i+};
   if ioresult <> 0 then exit else closefile(DepuisF);
  Reset(DepuisF, 1);
  Assignfile(DansF,chemhk804+'\'+actif+nom+'.mb');
  Rewrite(DansF, 1);
  REPEAT
    BlockRead(DepuisF,Tampon,
              SizeOf(Tampon),NumLus);
    BlockWrite(DansF,Tampon,NumLus,NumEcrits);
    {nb := nb + numecrits;}
  UNTIL (NumLus = 0) OR
        (NumEcrits <> NumLus);
  Closefile(DepuisF);
  Closefile(DansF);


screen.cursor := crdefault;
filelistbox2.directory := chemhk804;
filelistbox2.update;
filelistbox2.refresh;
filelistbox2.visible := false;
filelistbox2.enabled := false;
end;

0
 

Author Comment

by:PPaul
ID: 1341557
(Delphi informant volume 4, nr. 6)? are you talking about the books that came with delphi or the online help... I'm using D2 Desktop....
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341558
Hi PPaul,

nope Delphi Informant is just a delphi magazine (www.informant.com) (I stated there sentence on this thread)

If you need more info about FileStream, just ask.

I believe TFileStream is also described in the help-file.

Zif.

0
 

Author Comment

by:PPaul
ID: 1341559
sorry guys but I'm still at a lost and I'll have to meditate on the information that I recived. I wish that I could give the points to all of you but I can't. lgc5800 I would like to give them to you and also ask if I could get you e-mail adde so when I am ready I can talk to you about it...

Thanks

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341560
Hi PPaul,

 I see that you aren't convinst about using TFileStream. Well, I found something which maybe convinces you, look at this free class with source :

http://www.pablop.demon.co.uk/marley/tbufferedfilestream.htm

description :

TBufferedFileStream is a class derived from TFileStream that allows buffered single character input or output from a stream. It is handy if you are writing a parser that needs one character at a time and you don't want to worry about maintaining the buffer yourself. It performs file reads when you have read the entire buffer, otherwise, it supplies characters from the buffer.

Let me know what you think of this approach. I still think that TFileStream is better than the older ways of reading/writing from disk.

I'm not answering this q'n, because I first want to know if you want this.

Zif.
0
 

Author Comment

by:PPaul
ID: 1341561
Ok then.... How do I use it.... add it as a component or add it name to the user list at the top???
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341562
PPaul,

First you could examine the code. Then just copy it to a unit.
Then when you want to use it, just add that unit to your uses clausule.

What you also can do is just put the const, types, ... etc into your own form unit. But the first approach is better.

Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341563
PPaul, and working?
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 300 total points
ID: 1341564
If url and comments I gave you weren't helpfull, just reopen answer. Zif.
0
 

Author Comment

by:PPaul
ID: 1341565
Thanks.... I just need time from work to look at it closer.... but from what I see it's all I need Thanks alot ZefNab....

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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