Solved

The Blockread/BlockWrite

Posted on 1998-10-02
13
718 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Printing problem 2 102
Mobile Keyboard covers the display of the TMemo 3 96
Delphi: making a BW image transparent 10 62
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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