Recent

Author Topic: Open and Copy the executable file!  (Read 6803 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2254
    • Lazarus and Free Pascal italian community
Open and Copy the executable file!
« on: September 06, 2010, 08:10:33 am »
If I wanted to open an executable file and make a copy but reading byte by byte like I do?
In standard Pascal!  :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2254
    • Lazarus and Free Pascal italian community
Re: Open and Copy the executable file!
« Reply #1 on: September 06, 2010, 09:56:46 am »
In ansi C is:

FILE *fopen(const char *path, const char *mode);

mode="b" (binary mode open)

but in Pascal?!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Open and Copy the executable file!
« Reply #2 on: September 06, 2010, 10:00:48 am »
In general you can read here to learn how to use a lot of things in Pascal:

http://www.delphibasics.co.uk/Article.asp?Name=Files

On the specific topic there are two ways:

Code: [Select]
// Copies the file specified in the path ASource to ADest
// The file names are specified in UTF-8
procedure MyCopyFile(const ASource, ADest: string);
var
  SourceF, TargetF: File;         
  Buffer: array[1..2048] of Char;
  // The amount read and written for BlockRead and BlockWrite
  NumRead, NumWritten: Int64;
begin
  // Open the source file
  AssignFile(SourceF, UTF8ToSys(ASource));
  Reset(SourceF, 1); // Note that we specify the size the file records
  try
    // Now open the destination file
    AssignFile(TargetF, UTF8ToSys(ADest));
    Rewrite(TargetF, 1); // Note that we specify the size the file records
    repeat
      BlockRead(SourceF, Buffer, SizeOf(Buffer), NumRead); // Read ... and
      BlockWrite(TargetF, Buffer, NumRead, NumWritten); // copy the data
    until (NumRead = 0)
       or (NumWritten <> NumRead); // until there is nothing else to read
  finally                 
    CloseFile(SourceF);
    CloseFile(TargetF);
  end;
  ShowMessage('The file “' + OpenDialog1.FileName
    + '” was copied to “' + SaveDialog1.FileName + '”.');
end;

and using streams

Code: [Select]
procedure CopyFile(Source, Target: String);
var
  S: TFileStream = NIL;
  T: TFileStream = NIL;
begin
  S := TFileStream.Create(Source, fmOpenRead);
  try
    T := TFileStream.Create(Target, fmOpenWrite or fmCreate);
    T.CopyFrom(S, S.Size);
    FileSetDate(T.Handle, FileGetDate(S.Handle)); // Copy the date too
  finally
    if T <> NIL then T.Free;
    S.Free;
  end;
end;

A coincidence that I was just writing about this for the english Lazarus book.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8717
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Open and Copy the executable file!
« Reply #3 on: September 06, 2010, 10:39:34 am »
The standard Pascal (and the lowest level, also the hardest) way would be:
Code: [Select]
var
  f: File of Byte;
  b: Byte;
begin
  Assign(f,FileName);
  Reset(f);
  Read(f,b); // reads a single byte from the file
  Close(f);
end;

mvampire

  • Jr. Member
  • **
  • Posts: 62
Re: Open and Copy the executable file!
« Reply #4 on: September 06, 2010, 09:46:47 pm »
var MyFile, MyNewFile: text;
begin
AssignFile(MyFile,'name_of_my_file');
AssignFile(MyNewFile,'name_of_new_file');
Reset(MyFile);
Rewrite(MyNewFile);

While not EoF(MyFile) do begin
                                  Readln(MyFile,s);
                                  Writeln(MyNewFile,s);
                                  end;

CloseFile(MyFile);
CloseFile(MyNewFile);
end;

This program will copy MyFile to MyNewFile string by string.

Depnding on Your Pascal compiler, sometimes You should use Assign and Close instead AssignFile and Close File.

xinyiman

  • Hero Member
  • *****
  • Posts: 2254
    • Lazarus and Free Pascal italian community
Re: Open and Copy the executable file!
« Reply #5 on: September 07, 2010, 07:58:36 am »
Thank you all, I did thanks to your advice, but now I'd like to know how do I read file from bottom to reach the top of the file!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8717
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Open and Copy the executable file!
« Reply #6 on: September 07, 2010, 08:23:34 am »
Quote
how do I read file from bottom to reach the top of the file!
Well... you could first set the file pointer to the end of file with Seek method or FileSeek, but... what would you do that for?

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4370
  • I like bugs.
Re: Open and Copy the executable file!
« Reply #7 on: September 07, 2010, 09:24:32 am »
... and what might be the benefit of using a self made file copy function instead of CopyFile()?

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

xinyiman

  • Hero Member
  • *****
  • Posts: 2254
    • Lazarus and Free Pascal italian community
Re: Open and Copy the executable file!
« Reply #8 on: September 07, 2010, 09:42:10 am »
Actually I'm not creating a simple copy and paste! We put too much in the middle, I'm doing the experiments. In due course if these are successful will inform
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018