Solved

The Blockread/BlockWrite

Posted on 1998-10-02
13
709 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now