Jump to content

[PASCAL] File copying


CouldnoT
 Share

Recommended Posts

File copying

 

All file types, like text files, binary files, are based on byte units, which are the smallest representation of data in computer memory and on disk. Every file should contain one byte, two bytes, etc, or no bytes at all. Every byte could hold an integer or a character code from 0 to 255. We can open all file types using the File of Byte or File of Char declaration.

 

We can copy any file to another one using File of Byte files, and the result will be a new file that is identical to the source file's contents.

 

Copy files using file of byte

 

program FilesCopy;
{$mode objfpc}{$H+}
uses
 {$IFDEF UNIX}{$IFDEF UseCThreads}
 cthreads,
 {$ENDIF}{$ENDIF}
 Classes, SysUtils
 { you can add units after this };
var
 SourceName, DestName: string;
 SourceF, DestF: file of Byte;
 Block: Byte;
begin
 Writeln('Files copy');
 Write('Input source file name: ');
 Readln(SourceName);
 Write('Input destination file name: ');
 Readln(DestName);
 if FileExists(SourceName) then
 begin
 AssignFile(SourceF, SourceName);
 AssignFile(DestF, DestName);
 FileMode:= 0; // open for read only
 Reset(SourceF); // open source file
 Rewrite(DestF); // Create destination file
 // Start copy
 Writeln('Copying..');
 while not Eof(SourceF) do
 begin
 Read(SourceF, Block); // Read Byte from source file
 Write(DestF, Block); // Write this byte into new
 // destination file
 end;
 CloseFile(SourceF);
 CloseFile(DestF);
 end
 else // Source File not found
 Writeln('Source File does not exist');
 Write('Copy file is finished, press enter key to close..');
 Readln;
end.

 

After running the previous example, we should enter an existing source file and a new destination file. In Linux we could enter file names like this:

 

Input source file name: /home/motaz/quran/mishari/32.mp3
Input destination file name: /home/motaz/Alsajda.mp3

 

In Windows we could enter something like this:

 

Input source file name: c:\photos\mypphoto.jpg
Input destination file name: c:\temp\copy.jpg

 

If the source file exists in the same directory as the FileCopy program, we could enter only the file name like this:

 

Input source file name: test.pas
Input destination file name: testcopy.pas

 

If we use this method to copy large files, it will take a very long time compared with operating system copy procedures. That means the operating system uses a different technique to copy files. If we want to copy a 1 megabyte file, that means the while loop will repeat about 1 million times, that means a million read and a million write operations. If we replace the file of Byte declaration with file of Word, that means it will take about 500,000 cycles for read and write, but this will work only for the files whose size is even not odd. It will succeed if a file contains 1,420 bytes, but it will fail with a file of 1,423 bytes. To copy a file of any kind using a faster method, we should use untyped files.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.