home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / MISCTI10.ZIP / TI179.ASC < prev    next >
Encoding:
Text File  |  1988-04-18  |  4.1 KB  |  133 lines

  1. PRODUCT : TURBO PASCAL     NUMBER : 179
  2. VERSION : 3.0xx 
  3.      OS : ALL 
  4.    DATE : May 21, 1986
  5.  
  6.   TITLE : ALTERNATIVE FILE COPY ROUTINE 
  7.  
  8. program FileCopy;
  9. {
  10. This  is  a simple file copy program that illustrates how to  use 
  11. BlockRead  and  BlockWrite  to make an exact copy  of  any  file. 
  12.    
  13.    Note:  If  you   want  to adapt this program so that  it  will         
  14.           work on the CP/M 80 or CP/M 86 versions of Turbo Pascal          
  15.           you must do the following: 
  16.  
  17.        (1) Change the variables "SizeInBytes" and "LastByte" from 
  18.            real variables to integer variables.                        
  19.  
  20.        (2) Change  calls  to "LongFileSize"   and  "LongSeek"  to  
  21.            "FileSize" and "Seek."                                           
  22.                                                                             
  23.        (3) Take out  the call to "Trunc(LastByte)" and change  it 
  24.            to just "LastByte."                                           
  25. }
  26. const
  27.   BufSize = 128;                  { The buffer SizeInBlocks }
  28.  
  29. type
  30.                          { The type for the buffer variable }
  31.   BufType = array[1..BufSize] of byte;
  32. var
  33.   Source, Dest : file;         { The input and output files }
  34.  
  35.   SourceName,             { The input and output file names }
  36.   DestName : string[14];
  37.  
  38.   Buffer : BufType;        { The buffer for block transfers }
  39.  
  40.   SizeInBytes,           { The size in bytes of source file }
  41.  
  42.   LastByte : real;       { The position of the last byte in }
  43.                          { the last partially full buffer   }
  44.                          { read from the source file.       }
  45.  
  46.   SizeInBlocks,          { The size in blocks of the source }
  47.                          { file                             }
  48.  
  49.   Count : integer;         { A general use counter variable }
  50.  
  51.   FileExists : boolean; { Flag to test if source file exits }
  52.  
  53.  
  54.   ByteFile : file of byte;        { Used for byte transfers }
  55.  
  56. begin { FileCopy }
  57.   Write('Copy from: ');
  58.   Readln(SourceName);
  59.  
  60.   { Calculate the size in bytes of }
  61.   { the source file                }
  62.   Assign(ByteFile, SourceName);  
  63.   {$I-} Reset(ByteFile); {$I+}    
  64.   FileExists := (IOResult = 0);
  65.   SizeInBytes := LongFileSize(ByteFile);
  66.   Close(ByteFile);
  67.  
  68.   if FileExists then
  69.   begin
  70.     { Open the source file as an }
  71.     { untyped file               }
  72.     Assign(Source, SourceName);
  73.     {$I-} Reset(Source); {$I+}    
  74.     FileExists := (IOResult = 0); 
  75.  
  76.     { Calculate the size of the }
  77.     { source file in blocks     }
  78.     SizeInBlocks := FileSize(Source);    
  79.  
  80.     { Calculate the position of the   }
  81.     { last byte in the last partially }
  82.     { full buffer read with BlockRead }
  83.     LastByte := SizeInBytes -           
  84.              ((SizeInBlocks-1) * 128.0);
  85.   end;                                   
  86.   if FileExists then
  87.     begin
  88.       { Open the destination file }
  89.       Write('       To: '); 
  90.       Readln(DestName);
  91.       Assign(Dest, DestName);
  92.       Rewrite(Dest);
  93.  
  94.       { Copy all the full buffers to }
  95.       { the destination file         }
  96.       for Count := 1 to SizeInBlocks-1 do
  97.       begin                              
  98.         BlockRead(Source, Buffer, 1);    
  99.         BlockWrite(Dest, Buffer, 1);
  100.       end;
  101.  
  102.       { Read the last partially full }
  103.       { buffer                       }
  104.       BlockRead(Source, Buffer, 1);      
  105.  
  106.       { Close up the open files }
  107.       Close(Source); 
  108.       Close(Dest);
  109.  
  110.       { Open the destination file as a   }
  111.       { file of byte and seek to the end }
  112.       Assign(ByteFile, DestName); 
  113.       Reset(ByteFile);                   
  114.       LongSeek(ByteFile, LongFileSize(ByteFile));
  115.  
  116.       { Append the partially full buffer }
  117.       { to the end of the destination    }
  118.       { file and then close it           }
  119.       for Count := 1 to Trunc(LastByte) do
  120.         Write(ByteFile, Buffer[Count]);  
  121.       Close(ByteFile);                  
  122.     end                                  
  123.  
  124.   else
  125.     { Issue an error message if an }
  126.     { error occured on the reset   }
  127.     Writeln('An error occurred when resetting "', SourceName,
  128. '".');
  129. end. { FileCopy }
  130.  
  131.  
  132.  
  133.