Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 746
  • Last Modified:

The Blockread/BlockWrite

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
PPaul
Asked:
PPaul
1 Solution
 
ZifNabCommented:
PPaul, what exactly do you want to get, because TFileStream is also a way to get good file manipulation. Zif.
0
 
ZifNabCommented:
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
 
titzCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lgc5800Commented:
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
 
PPaulAuthor Commented:
(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
 
ZifNabCommented:
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
 
PPaulAuthor Commented:
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
 
ZifNabCommented:
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
 
PPaulAuthor Commented:
Ok then.... How do I use it.... add it as a component or add it name to the user list at the top???
0
 
ZifNabCommented:
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
 
ZifNabCommented:
PPaul, and working?
0
 
ZifNabCommented:
If url and comments I gave you weren't helpfull, just reopen answer. Zif.
0
 
PPaulAuthor Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now