Mega Code Archive

 
Categories / Delphi / Procedures
 

Write - write data to a binary or text file system unit

1 procedure Write ( Expression1 {options} {, Expression2 {options} ...} ) ; 2 procedure Write ( var FileHandle : TextFile; Expression1 {options} {, Expression2 {options} ...} ) ; 3 procedure Write ( var FileHandle : File; var Value1 {,var Value2 ...} ) ; Description The Write procedure writes a single line of data to a file or to the console. Version 1 Is used to write a line of text to the console. Version 2 Is used to write a line of text to a text file with the given FileHandle. Version 3 Is used to write a line of data to a binary file with the given FileHandle. You must use AssignFile to assign a file to the FileHandle and open the file with Reset or ReWrite before using Write. For text files, the text written may be any valid Expression(s). Often these will be strings, but may also be expressions that result in strings or numbers. After each expression, you can add formatting options: :width Field width for strings + numbers :precision Decimal digits for numbers For typed binary files, data values Value1, Value2 etc, are written as data lines to the file. These values must be of the same type as the file. Notes You cannot use Write to write to an untyped binary file (one declared as File with no following of type). To write to a binary file, use BlockWrite. Write does not buffer records, so it is more efficient to use BlockWrite. Write is also a Delphi directive. It is used with the Property keyword. Related commands AssignFile Assigns a file handle to a binary or text file BlockRead Reads a block of data records from an untyped binary file BlockWrite Writes a block of data records to an untyped binary file File Defines a typed or untyped file Read Read data from a binary or text file ReadLn Read a complete line of data from a text file Reset Open a text file for reading, or binary file for read/write ReWrite Open a text or binary file for write access TextFile Declares a file type for storing lines of text WriteLn Write a complete line of data to a text file Example code : Illustrating single, multiple and formatted text line writing var myFile : TextFile; text : string; i : Integer; begin // Try to open the Test.txt file for writing to AssignFile(myFile, 'Test.txt'); ReWrite(myFile); // Write a couple of well known words to this file Write(myFile, 'Hello '); Write(myFile, 'World'); // Terminate this line WriteLn(myFile); // Write some numbers to the file as a single line for i := 2 to 4 do Write(myFile, i/2, ' '); // Terminate this line WriteLn(myFile); // Repeat the above, but with number formatting for i := 2 to 4 do Write(myFile, i/2:5:1); // Terminate this line WriteLn(myFile); // Close the file CloseFile(myFile); // Reopen the file for reading only Reset(myFile); // Display the file contents while not Eof(myFile) do begin ReadLn(myFile, text); ShowMessage(text); end; // Close the file for the last time CloseFile(myFile); end; Show full unit code Hello World 1.00000000000000E+0000 1.50000000000000E+0000 2.00000000000000E+0000 1.0 1.5 2.0 Example code : Write typed records to a record type binary file type TCustomer = record name : string[20]; age : Integer; male : Boolean; end; var myFile : File of TCustomer; // A file of customer record customer : TCustomer; begin // Try to open the Test.cus binary file for writing to AssignFile(myFile, 'Test.cus'); ReWrite(myFile); // Write a couple of customer records to the file customer.name := 'Fred Bloggs'; customer.age := 21; customer.male := true; Write(myFile, customer); customer.name := 'Jane Turner'; customer.age := 45; customer.male := false; Write(myFile, customer); // Close the file CloseFile(myFile); // Reopen the file in read only mode FileMode := fmOpenRead; Reset(myFile); // Display the file contents while not Eof(myFile) do begin Read(myFile, customer); if customer.male then ShowMessage('Man with name '+customer.name+ ' is '+IntToStr(customer.age)) else ShowMessage('Lady with name '+customer.name+ ' is '+IntToStr(customer.age)); end; // Close the file for the last time CloseFile(myFile); end; Show full unit code Man with name Fred Bloggs is 21 Lady with name Jane Turner is 45 Example code : Writing to a Word type binary file var myWord, myWord1, myWord2 : Word; myFile : File of Word; begin // Try to open the Test.bin binary file for writing to AssignFile(myFile, 'Test.bin'); ReWrite(myFile); // Write a couple of lines of Word data to the file myWord1 := 234; myWord2 := 567; Write(myFile, myWord1, myWord2); // Close the file CloseFile(myFile); // Reopen the file in read only mode FileMode := fmOpenRead; Reset(myFile); // Display the file contents while not Eof(myFile) do begin Read(myFile, myWord); ShowMessage(IntToStr(myWord)); end; // Close the file for the last time CloseFile(myFile); end; Show full unit code 234 567