community.borland.com

Article #15721: RESET - READING AND WRITING TYPED UNTYPED FILES

 Technical Notes Database

TN721D.txt   RESET - READING AND WRITING TYPED UNTYPED FILES
Category   :Pascal
Platform    :All
Product    :Turbo Pascal  4.0+

Description:
Q.  How can I write to a binary file without destroying the
   information already in it?
A.  You must use the Reset procedure.  It opens the file for BOTH
   reading and writing.  Furthermore, the RecordSize*Count cannot
   exceed the total size of the buffer.  Therefore, you may have
   to use

       Reset(FileVariable, 1)  { RecordSize = 1 }.

   Now, you may use the Seek procedure, to go to a particular
   position in the file, and the BlockWrite procedure to write
   over some data.  The following example demonstrates the use of
   BlockRead and BlockWrite:

   program ReadWriteFile;
   var
    FromF : file;
    NumRead, NumWritten: Word;
    Buf : array[1..2] of Char;
    Ch : Char;
   begin
    { Open input file and create it }
    Assign(FromF, 'MyFile.Dat');
    { Record size = 1 }
    Buf[1] := 'a';  Buf[2] := 'b';
    Ch := 'c';
    Rewrite(FromF,1);
    BlockWrite(FromF,Ch,SizeOf(Ch),NumWritten);
    Close(FromF);
    Reset(FromF, 1);
    { Open output file and write to it and read from it }
    BlockWrite(FromF,Buf,SizeOf(Buf),NumWritten);
    BlockRead(FromF,Ch,SizeOf(Ch),NumRead);
    Close(FromF);
   end.


Reference:


7/16/98 4:35:35 PM
 

Last Modified: 01-SEP-99