Working with Untyped Files

Untyped files are files that have no structure. Basically, untyped files are typed files that use bytes instead of records. The declaration of an untyped file variable looks like this:

var   F: file;

The calls to the Reset and Rewrite procedures are a bit different when you're working with untyped files. Normally, both procedures use 128 bytes as the default record size. When you're working with untyped files, you should set this size to 1 byte. You can do this by passing 1 as the second parameter in both calls:

Reset(F, 1); Rewrite(F, 1);

To read data from and write data to typed files, you have to use the BlockRead and BlockWrite procedures. Here are the declarations of these procedures:

procedure BlockRead(var F: File; var Buf;   Count: Integer [; var AmtTransferred: Integer]); procedure BlockWrite(var F: File; var Buf;   Count: Integer [; var AmtTransferred: Integer]);

The first parameter is the untyped file variable that is used to access the file on disk. The second parameter is a buffer that the procedures use to transfer data to and from the file. This buffer is usually a static array of bytes, but it can also be a record. The third parameter specifies how many bytes to transfer. This is usually the size of the array, which can easily be determined with the SizeOf function. The optional AmtTransferred variable parameter can be used to track the exact number of bytes transferred to and from the file.

The application in Listing 8-7 uses the BlockRead and BlockWrite procedures to enable the user to copy files. The application also enables the user to specify the source and destination file names in the command line (see Figure 8-2).

image from book
Figure 8-2: Passing parameters to the application

To see how many parameters the user passed to your application, you can use the ParamCount function. ParamCount takes no parameters and returns 0 if the user called the application with no additional parameters.

To read application parameters, you have to use the ParamStr function. The ParamStr function accepts a single Integer parameter: the parameter's index. If you pass 0 to the ParamStr function, it will return the path and file name of your application. Custom parameters, if there are any, start at index 1.

Listing 8-7: Copying files using BlockRead and BlockWrite

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; procedure BlockCopyFile(const SrcPath, DestPath: string); var   Src: file;   Dest: file;   Buffer: array[1..1024] of Byte;   BytesRead: Integer; begin   if LowerCase(SrcPath) = LowerCase(DestPath) then Exit;   AssignFile(Src, SrcPath);   {$I-}   Reset(Src, 1);   {$I+}   if IOResult = 0 then   begin     AssignFile(Dest, DestPath);     {$I-}     Rewrite(Dest, 1);     {$I+}     if IOResult = 0 then     begin       BytesRead := -1;       while BytesRead <> 0 do       begin         BlockRead(Src, Buffer, SizeOf(Buffer), BytesRead);         BlockWrite(Dest, Buffer, BytesRead);       end;       CloseFile(Dest);       WriteLn('File successfully copied.');     end;         // if Dest Rewrite     CloseFile(Src);   end;          // if Source Reset end; var   SourcePath: string;   DestPath: string; begin   { Accept parameters }   if ParamCount = 2 then     BlockCopyFile(ParamStr(1), ParamStr(2))   else begin     { if there are no parameters in the command line,       ask the user to enter filenames here }     Write('Source path: ');     ReadLn(SourcePath);     Write('Destination path: ');     ReadLn(DestPath);     if (SourcePath <> '') and (SourcePath <> '') then       BlockCopyFile(SourcePath, DestPath);   end;   WriteLn('Press Enter to exit.');   ReadLn; end.
image from book

The application first checks whether the user passed two parameters in the command line. If the user passed two parameters, ParamStr(1) contains the path of the source file and ParamStr(2) contains the path of the destination file.

The copying is done in the BlockCopyFile procedure. The first line:

if LowerCase(SrcPath) = LowerCase(DestPath) then Exit;

uses the LowerCase function to temporarily convert both file names to lower- case and tests whether both file names point to the same file. If destination and source are equal, then there is no need to perform the copy and the if-then statement calls Exit to exit from the procedure.

The main part of the BlockCopyFile procedure is the while loop that calls the BlockRead and BlockWrite procedures:

while BytesRead <> 0 do begin   BlockRead(Src, Buffer, SizeOf(Buffer), BytesRead);   BlockWrite(Dest, Buffer, BytesRead); end;

The BlockRead procedure reads 1 KB of data at a time from the file, writes the data into the Buffer array, and updates the BytesRead variable, which always holds the exact amount of bytes transferred. The loop will iterate as long as the BlockRead procedure continues reading data from the file. When the BlockRead procedure reaches the end of the file, the BytesRead value will drop to 0, the condition of the while loop will be met, and the copying of the file will finish.

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: