TURBO PASCAL

Новости

Программы   

Turbo Pascal 

Игры

Документация   

Странности

FAQ

Ссылки

Форум

Гостевая книга

Рассылка

Благодарности

Об авторе

62 How can I copy a file in a Turbo Pascal program?

 A: Here is the code. 
Take a close look. It has some instructive
features besides the copying, like handling the filemode and using
dynamic variables (using pointers).
  procedure SAFECOPY (fromFile, toFile : string);
  type bufferType = array [1..65535] of char;
  type bufferTypePtr = ^bufferType;  { Use the heap }
  var bufferPtr : bufferTypePtr;     { for the buffer }
      f1, f2 : file;
      bufferSize, readCount, writeCount : word;
      fmSave : byte;              { To store the filemode }
  begin
    bufferSize := SizeOf(bufferType);
    if MaxAvail < bufferSize then exit;  { Assure there is enough 
memory }
    New (bufferPtr);              { Create the buffer }
    fmSave := FileMode;           { Store the filemode }
    FileMode := 0;                { To read also read-only files }
    Assign (f1, fromFile);
    {$I-} Reset (f1, 1); {$I+}    { Note the record size 1, important! }
    if IOResult <> 0 then exit;   { Does the file exist? }
    Assign (f2, toFile);
    {$I-} Reset (f2, 1); {$I+}    { Don't copy on an existing file }
    if IOResult = 0 then begin close (f2); exit; end;
    {$I-} Rewrite (f2, 1); {$I+}  { Open the target }
    if IOResult <> 0 then exit;
    repeat                        { Do the copying }
      BlockRead (f1, bufferPtr^, bufferSize, readCount);
      {$I-} BlockWrite (f2, bufferPtr^, readCount, writeCount); {$I+}
      if IOResult <> 0 then begin close (f1); exit; end;
    until (readCount = 0) or (writeCount <> readCount);
    writeln ('Copied ', fromFile, ' to ', toFile,
             ' ', FileSize(f2), ' bytes');
    close (f1); close (f2);
    FileMode := fmSave;           { Restore the original filemode }
    Dispose (bufferPtr);          { Release the buffer from the heap }
  end;  (* safecopy *)
Of course a trivial 
solution would be to invoke the MS-DOS copy
command using the Exec routine. (See the item "How do I 
execute an
MS-DOS command from within a TP program?")

 Contents

 

На первую страницу

Rambler's Top100 Rambler's Top100
PROext: Top 1000

(с)Все права защищены

По всем интересующим вопросам прошу писать на электронный адрес

Hosted by uCoz