StartprogrammingusingObjectPascal:CopyFilesUsingFileOfByte

From 흡혈양파의 번역工房
Revision as of 09:57, 26 July 2012 by Onionmixer (talk | contribs) (SPOP FileOfByte를사용하여파일복사하기 페이지 추가)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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.

앞의 예제를 실행하면, 기존의 파일 이름과 새 목적 파일 이름을 입력할 것입니다. 리눅스에서는 파일 이름들을 다음 처럼 입력할 수 있습니다.

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

윈도우즈에서는 다음 처럼 입력할 수 있습니다.

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

만약 FileCopy 프로그램과 같은 디렉터리에 원본 파일이 있다면, 다음 처럼 파일 이름만 입력할 수 있습니다.

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

큰 파일을 복사하기 위해 이 방법을 사용하면, 운영체제의 복사 프로시저와 비교해서 매우 많은 시간이 걸릴 것입니다. 이는 운영체제가 파일을 복사하기 위해 다른 기술을 사용한다는 것을 의미합니다. 만약 1메가바이트 파일을 복사하려 한다면, 1백만 번 while 순환문을 반복해야 한다는 것인데, 이는 곧 백만 번 읽고 백만 번 쓰는 동작을 수행한다는 것입니다. file of Byte를 file of Word 선언으로 대체하면, 읽기 쓰기를 500,000 번 수행하는 의미가 되지만, 파일 크기가 홀수가 아닌 짝수일 경우에만 동작할 것입니다. 만약 파일이 1,420 바이트를 가지고 있다면 잘 동작하겠지만, 1,423 바이트를 가지고 있다면 실패할 것입니다.

더 빠른 방법으로 어떤 종류의 파일이든 복사하기 위해, 비형식화 된 파일을 사용할 것입니다.