(Post) how to operate on File Reading and Writing in Delphi?

Source: Internet
Author: User

How to operate in DelphiCompositionReading and writing?

I was just getting started with Delphi. How can I read and write files in Delphi? Thank you very much for your advice! It is best to refer to the source code.


VaR F: text;
...
Assignfile (F, 'c: \ 1.txt ');
Rewrite (f );
Writeln (F, 'This is a test ');
Closefile (f );
...
Rewrite is a new file. If it is changed to reset, the file is opened.
Writeln is the row write and line feed, write is the write without line feed, readln is the read row and line feed, read is the read but not the line feed


There are many methods to read and write files in Delphi:
I will post to you:
Program During the design, we often encounter the need to process files, directories and drives. This chapter describes how to deal with different types
File. This chapter includes how to use t f I l e S t r e a m class to encapsulate the input/output of files and how to use the three-bit wi n d o w s main feature-memory ing file. You will learn how to create a T m e m o r y m a p e d f I L E class that encapsulates the memory ing file function, use it to query text in text files. This chapter also provides methods for selecting a drive, querying files in a tree directory, and obtaining file version information. This chapter gives you an in-depth 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.
. A text file is the s c I text that can be read by any text editor. A type file is a file that contains a data type defined by a programmer. A binary file contains all other types. It is a general term for a file that contains any format or unformatted data.
Text File Processing
This section describes how to process text files using the process and functions built in the Object Pascal Runtime Library. Open a text file before processing it. Declare a variable: this variable can be used to reference a text file.
Two steps are required to open a file: first, a s I g n f I L E (), which can associate the file variables with a file. For example, you can perform Step 2: open the file. There are three ways to open a file: Use the r e w r I t e () process to create and open the file; for an existing file, this operation will overwrite the file; use the r e s e t () process to open a file read-only; Use the append () process to append text to an existing file.
Note that reset () will open type files and non-type files in read/write mode. You can use c l o s e f I L E () to close an opened file. The following example shows various called functions.
File opening and closing
Assignfile: Associate an external file name with a file variable
Reset: open an existing file
Rewrite: Create and open a new file (or overwrite the original file)
Append: open a file by adding it (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: closes a file with a specific handle.
The following three files are mainly used inside the system and are often used in programming file replication. The objects they operate on are file handles rather than file variables.
File Location
Seek: Move the current file location to the specified part
Filepos: the current location of the returned File
Eoln: indicates the end of the returned row.
EOF: indicates the end of the returned file.
FileSeek: Change the pointer position of the current file


Another question is, how can I determine whether the file has been opened? Do you want to close the file after reading it, and open it again next time you read or write it?


Normally, this is the case.


Use tfilestream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.