DelphiFan Magazine

Top Menu

Main Menu

  • DELPHI
  • CODE SAMPLES
  • FIREMONKEY
  • DATABASE
  • RELEASES
  • VIDEOS
  • REVIEW
  • TECH NEWS

logo

DelphiFan Magazine

  • DELPHI
  • CODE SAMPLES
  • FIREMONKEY
  • DATABASE
  • RELEASES
  • VIDEOS
  • REVIEW
  • TECH NEWS
  • How to add a server application written in UNIGUI to startup?

  • UniGUI Expiry Link Anonymizer

  • UniGUI Add CAPTCHA to the WEB project

  • UniGui. Unique hybrid of Delphi and JS

  • How to transform a blob field into string lines (varchar) in SQL Firebird?

CODE SAMPLESDELPHI
Home›CODE SAMPLES›How does Delphi handle different types of files

How does Delphi handle different types of files

By admin
November 19, 2020
1674
0
Share:

In program design, we often encounter situations where we need to deal with files, directories and drives. Here we will describe how to deal with different types of files.   

Mainly includes how to use the TFileStream class to encapsulate the input/output of files and how to use the main feature of 32-bit windows-memory mapped files. You will learn how to create a TMemoryMappedFile class that encapsulates the memory-mapped file function and use it to perform text queries in text files. It also provides some methods for selecting a drive, querying files in a tree directory, and obtaining file version information. Make you have a deep understanding of files, directories and drives.

Process file input/output

You will process three types of files: text files, type files, and binary files   

The following sections are about file input/output. Text files are ASCII texts that can be read by any text editor. Type files refer to files containing data types defined by the programmer. Binary files include all other types. It is a general term for files containing arbitrary format or unformatted data.

Processing of text files

This article describes how to process text files using procedures and functions built into the Object Pascal runtime library. Before any processing on the text file, the text file must be opened first. Declare variable: A text file can be referenced through this variable.

Opening a file requires two steps: The first is AssignFile(), which can associate file variables with a file. Then proceed to the second step: open the file. There are three ways to open a file: use the ReWrite() process to create and open the file. For an existing file, this operation will cause the file to be overwritten; use the Reset() process to open a file read-only; use the Append() process to Append text to existing files.

Note that Reset() will open type files and untyped files in read-write mode. You can use CloseFile() to close an open file

File opening and closing

AssignFile Associate an external file name with a file variable
Reset Open an existing file, text file (read only), other files (read and write)
ReWrite Create and open a new file (or overwrite an existing file)
Append Open a file in append mode (only applicable to text files)
CloseFile Close an open file
FileOpen Open a specific file and return the file handle
FileCreate Create a file with a given file name and return the file handle
FileClose Close a file with a specific handle

The following three files are mainly for internal use of the system, and are often used in the programming process of file copying. The objects they operate on are file handles, not file variables.

File location

Seek Move the current position of the file to the specified part
FilePos Returns the current position of the file
Eoln Return line end flag
EOF Return end of file mark
FileSeek Change the position of the current file pointer

There is another question, how to judge the file has been opened? Should I close the file after reading and writing, and open it again when I read or write next time?

Use TFileStream

For file operations through Byte arrays, they are often used in FTP. I also found these two functions when Delphi called Web Service to upload and download files. They are very easy to use and I recommend them to everyone. (Statement: Not written by me)

<1>Generate Byte array into file

procedure ByteArrayToFile(Const ByteArray : TByteDynArray; Const FileName : String);
var
    Conut : Integer;
    F : File of Byte;
    pTemp : Pointer;
begin
    AssignFile(F, FileName);
    ReWrite(F);
    try
        Count := Length(ByteArray);
        pTemp := @ByteArray[0];
        BlockWrite(F, pTemp^, Count);
    finally
        CloseFile(F);
    end;
end;
 
<2>Generate file into Byte array
 
function FileToByteArray(const FileName : String) :TButeBtnArray;
const
    BLOCK_SIZE = 1024;
var
    BytesRead, BytesToWrite, Count :Integer;
    F :Filw of Byte;
    pTemp : Pointer;
begin
    AssignFile(F, FileName);
    Reset(F);
    try
        Count := FileSize(F);
        SetLength(Result, Count);
        pTemp := @Result[0];
        BytesRead := BLOCK_SIZE;
        while(BytesRead = BLOCK_SIZE) do
        begin
            BytesToWrite := Min(Count, BLOCK_SIZE);
            BlockRead(F, pTemp^, BytesToWrite, BytesRead);
            pTemp := Pointer(LongInt(pTemp)+BLOCK_SIZE);
            Count := Count-BytesRead;
        end;
    finally
        CloseFile(F);
    end;
end;
 
function Encrypt(mStr : String; mKey : String) :String;
var
    I, J :Integer;
begin
    J:=1;
    Result := '';
    for I:=1 to Length(mStr) do begin
        Result := Result + Char(Ord(mStr[I] xor Ord(mKey[j]));
        if J + 1 <= Length(mKey) then
            Inc(J)
        else
            J:=1;  
    end;
    {Add steps yourself}
end;
  
function Decrypt(mStr : String; mKey : String) :String;
var
    I,J: Integer;
begin
    J :=1;
    Result := '';
    {Add steps yourself}
    for I :=1 to Length(mStr) do begin
        Result := Result + Char(Ord(mStr[I] xor Ord(mKey[J]));
        if J+1 <= Length(mKey) then;
            Inc(J)
        else
            J:=1;
    end;
end;
  
procedure TForm1.Button1Click(Sender : TObject);
const
    cKey1 = 'Who wants to try how to break';
    cKey2 = 'I don’t want to play this way (1) I don’t want to play this way (2) I don’t want to play this way (3)';
    cKey3 = 'Memo2.Text := Decrypt(Encrypt(Memo1.Text, cKey), cKey);';
var
    S :String;    //Encrypted characters
begin
    S := Encrypt(Encrypt(Encrypt(Memo1.Text, cKey1), cKey2), cKey3);
    ShowMessage(S);
    Memo2.Text := Decrypt(Decrypt(Decrypt(S, cKey3), cKey2), cKey1);
end;
 
 
Tagsdelphidelphi different filesdelphi filesfilesread files
Previous Article

UniGUI special objects

Next Article

About memory data and JSON

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0
  • 0

admin

Related articles More from author

  • CODE SAMPLESDELPHI

    UniGUI Expiry Link Anonymizer

    January 27, 2021
    By admin
  • DELPHIREVIEW

    Gnostice Document Studio Delphi

    September 29, 2020
    By admin
  • CODE SAMPLESDELPHI

    Rapidly Use Delphi FireDAC In A Multithreaded Environment With The FireDAC.Pooling Sample

    September 14, 2020
    By admin
  • DELPHIVIDEOS

    Android Permissions in Delphi

    October 2, 2020
    By admin
  • DELPHIVIDEOS

    Per Form VCL Styling

    September 29, 2020
    By admin
  • DELPHI

    Where to download OpenSSL binaries?

    January 3, 2021
    By admin

Leave a reply Cancel reply

You may interested

  • CODE SAMPLESDELPHI

    Connect And Manage A SQLite Database For Delphi

  • CODE SAMPLESDELPHI

    Customize the appearance of uniGUI TreeMenu

  • CODE SAMPLESDELPHI

    Add a column of record number to uniGUI’s table control uniDBGrid

  • LATEST REVIEWS

  • TOP REVIEWS

Timeline

  • January 27, 2021

    How to add a server application written in UNIGUI to startup?

  • January 27, 2021

    UniGUI Expiry Link Anonymizer

  • January 26, 2021

    UniGUI Add CAPTCHA to the WEB project

  • January 26, 2021

    UniGui. Unique hybrid of Delphi and JS

  • January 13, 2021

    How to transform a blob field into string lines (varchar) in SQL Firebird?

Latest Comments

Find us on Facebook

Follow Us on Instagram

logo

Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.

About us

  • 660 Pennsylvania Avenue Southeast #100 Washington, DC 20003
  • 0123456789
  • adsdelphi@gmail.com
  • Recent

  • Popular

  • Comments

  • How to add a server application written in UNIGUI to startup?

    By admin
    January 27, 2021
  • UniGUI Expiry Link Anonymizer

    By admin
    January 27, 2021
  • UniGUI Add CAPTCHA to the WEB project

    By admin
    January 26, 2021
  • UniGui. Unique hybrid of Delphi and JS

    By admin
    January 26, 2021
  • How to add a server application written in UNIGUI to startup?

    By admin
    January 27, 2021
  • Amazing Cross Platform Email App Sample In Delphi 10.4.1 FireMonkey For Android And IOS

    By admin
    August 13, 2020
  • Critical Update Available For Delphi FireMonkey 10.4.1 On Android, IOS, OSX, Windows, And Linux

    By admin
    September 4, 2020
  • Setting up the IDE for your first Android Application in Delphi

    By admin
    September 7, 2020

Follow us

Find us on Facebook

© Copyright Delphifan Forum. All rights reserved.