Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


Lecture notes, cheat sheets
Free library / Directory / Lecture notes, cheat sheets

Computer science and information technology. Dynamic memory (lecture notes)

Lecture notes, cheat sheets

Directory / Lecture notes, cheat sheets

Comments on the article Comments on the article

Table of contents (expand)

LECTURE No. 6. Files

1. Files. File operations

The introduction of the file type into the Pascal language is caused by the need to provide the ability to work with peripheral (external) computer devices designed for input, output and data storage.

The file data type (or file) defines an ordered collection of an arbitrary number of components of the same type. The common property of an array, set, and record is that the number of their components is determined at the stage of writing the program, while the number of file components in the program text is not determined and can be arbitrary.

When working with files, I / O operations are performed. An input operation means transferring data from an external device (from an input file) to the main memory of a computer, an output operation is a transfer of data from the main memory to an external device (to an output file). Files on external devices are often referred to as physical files. Their names are determined by the operating system.

In Pascal programs, filenames are specified using strings. To work with files in the program, you must define a file variable. Pascal supports three file types: text files, component files, untyped files.

File variables that are declared in a program are called logical files. All the basic procedures and functions that provide data I/O work only with logical files. The physical file must be associated with the logical file before the file opening procedures can be performed.

Text files

A special place in the Pascal language is occupied by text files, the components of which are of character type. To describe text files, the language defines the standard type Text:

var TF1, TF2: Text;

Text files are a sequence of lines, and lines are a sequence of characters. Lines are of variable length, each line ends with a line terminator.

Component Files

A component or typed file is a file with the declared type of its components. Component files consist of machine representations of variable values; they store data in the same form as computer memory.

The description of the file type values ​​is:

type M = File Of T;

where M is the name of the file type;

T - component type.

File components can be all scalar types, and from structured types - arrays, sets, records. In almost all specific implementations of the Pascal language, the "file of files" construct is not allowed.

All operations on component files are performed using standard procedures.

Write(f,X1,X2,...XK)

Untyped files

Untyped files allow you to write arbitrary sections of computer memory to disk and read them from disk to memory. Untyped files are described as follows:

var f: File;

Now we list the procedures and functions for working with different types of files.

1. Procedure Assign(var F; FileName: String);

The AssignFile procedure maps an external file name to a file variable.

F is a file variable of any file type, FileName is a String expression, or a PChar expression if extended syntax is allowed. All further operations with F are performed with an external file.

You cannot use a procedure with an already open file variable.

2. Procedure Close(varF);

The procedure breaks the link between the file variable and the external disk file and closes the file.

F is a file variable of any file type, opened by the Reset, Rewrite, or Append procedures. The external file associated with F is fully modified and then closed, freeing the file descriptor for reuse.

The {SI+} directive allows you to handle errors during program execution using exception handling. With the {$1-} directive turned off, you must use IOResult to check for I/O errors.

3.Function Eof(var F): Boolean;

{Typed or untyped files}

Function Eof[(var F: Text)]: Boolean;

{text files}

Checks whether or not the current file position is the end of the file.

Eof(F) returns True if the current file position is after the last character of the file, or if the file is empty; otherwise Eof(F) returns False.

The {SI+} directive allows you to handle errors during program execution using exception handling. With the {SI-} directive turned off, you must use IOResult to check for I/O errors.

4. Procedure Erase(var F);

Deletes the external file associated with F.

F is a file variable of any file type.

Before calling the Erase procedure, the file must be closed.

The {SI+} directive allows you to handle errors during program execution using exception handling. With the {SI-} directive turned off, you must use IOResult to check for I/O errors.

5. Function FileSize(var F): Integer;

Returns the size in bytes of file F However, if F is a typed file, FileSize will return the number of records in the file. The file must be open before using the FileSize function. If the file is empty, FileSize(F) returns zero. F is a variable of any file type.

6.Function FilePos(var F): LongInt;

Returns the current position of a file within a file.

Before using the FilePos function, the file must be open. The FilePos function is not used with text files. F is a variable of any file type, except for the Text type.

7. Procedure Reset(var F [: File; RecSize: Word]);

Opens an existing file.

F is a variable of any file type associated with an external file using AssignFile. RecSize is an optional expression that is used if F is an untyped file. If F is an untyped file, RecSize determines the record size that is used when transferring data. If RecSize is omitted, the default record size is 128 bytes.

The Reset procedure opens an existing external file associated with file variable F. If there is no external file with that name, a run-time error occurs. If the file associated with F is already open, it is first closed and then reopened. The current file position is set to the beginning of the file.

8. Procedure Rewrite(var F: File [; Recsize: Word]);

Creates and opens a new file.

F is a variable of any file type associated with an external file using AssignFile. RecSize is an optional expression that is used if F is an untyped file. If F is an untyped file, RecSize determines the record size that is used when transferring data. If RecSize is omitted, the default record size is 128 bytes.

The Rewrite procedure creates a new external file with the name associated with F. If an external file with the same name already exists, it is deleted and a new empty file is created.

9. Procedure Seek(var F; N: LongInt);

Moves the current file position to the specified component. You can only use the procedure with open typed or untyped files.

The current position of file F is moved to number N. The number of the first component of the file is 0.

The Seek(F, FileSize(F)) instruction moves the current file position to the end of the file.

10. Procedure Append(var F: Text);

Opens an existing text file to append information to the end of the file (append).

If an external file with the given name does not exist, a run-time error occurs. If file F is already open, it closes and reopens. The current file position is set to the end of the file.

11.Function Eoln[(var F: Text)]: Boolean;

Checks if the current file position is the end of a line in a text file.

Eoln(F) returns True if the current file position is at the end of a line or file; otherwise Eoln(F) returns False.

12. Procedure Read(F, V1 [, V2,..., Vn]);

{Typed and untyped files}

Procedure Read([var F: Text;] V1 [, V2,..., Vn]);

{text files}

For typed files, the procedure reads the file component into a variable. On each read, the current position in the file advances to the next element.

For text files, one or more values ​​are read into one or more variables.

With String variables, Read reads all characters up to (but not including) the next end-of-line marker, or until Eof(F) evaluates to True. The resulting character string is assigned to the variable.

In the case of a variable of an integer or real type, the procedure waits for a sequence of characters that form a number according to the rules of Pascal syntax. Reading stops when the first space, tab, or end-of-line is encountered, or when Eof(F) evaluates to True. If the numeric string does not match the expected format, then an I/O error occurs.

13. Procedure Readln([var F: Text;] V1 [, V2..., Vn]);

It is an extension of the Read procedure and is defined for text files. Reads a string of characters in the file, including the end-of-line marker, and moves to the beginning of the next line. Calling the Readln(F) function with no parameters moves the current file position to the beginning of the next line, if there is one, otherwise it jumps to the end of the file.

14. Function SeekEof[(var F: Text)]: Boolean;

Returns the end-of-file and can only be used for open text files. Typically used to read numeric values ​​from text files.

15. Function SeekEoln[(var F: Text)]: Boolean;

Returns the line terminator in a file and can only be used for open text files. Typically used to read numeric values ​​from text files.

16. Procedure Write([var F: Text;] P1 [, P2,..., Pn]);

{text files}

Writes one or more values ​​to a text file.

Each entry parameter must be of type Char, one of the integer types (Byte, ShortInt, Word, Longint, Cardinal), one of the floating point types (Single, Real, Double, Extended, Currency), one of the string types (PChar, AisiString , ShortString), or one of the boolean types (Boolean, Bool).

Procedure Write(F, V1,..., Vn);

{Typed files}

Writes a variable to a file component. Variables VI...., Vn must be of the same type as the file elements. Each time a variable is written, the current position in the file is moved to the next element.

17. Procedure Writeln([var F: Text;] [P1, P2,..., Pn]);

{text files}

Performs a Write operation, then places an end-of-line marker in the file.

Calling Writeln(F) with no parameters writes an end-of-line marker to the file. The file must be open for output.

2. Modules. Types of modules

A module (1Ж1Т) in Pascal is a specially designed library of subroutines. A module, unlike a program, cannot be launched on its own, it can only participate in building programs and other modules. Modules allow you to create personal libraries of procedures and functions and build programs of almost any size.

A module in Pascal is a separately stored and independently compiled program unit. In general, a module is a collection of software resources intended for use by other programs. Program resources are understood as any elements of the Pascal language: constants, types, variables, subroutines. The module itself is not an executable program, its elements are used by other program units.

All program elements of the module can be divided into two parts:

1) program elements intended for use by other programs or modules, such elements are called visible outside the module;

2) software elements that are necessary only for the operation of the module itself, they are called invisible (or hidden).

In accordance with this, the module, in addition to the header, contains three main parts, called interface, executable and initialized.

In general, a module has the following structure:

unit <module name>; {module title}

interface

{description of visible program elements of the module}

implementation

{description of the hidden programming elements of the module}

begin

{module element initialization statements}

end.

In a particular case, the module may not contain an implementation part and an initialization part, then the module structure will be as follows:

unit <module name>; {module title}

interface

{description of visible program elements of the module}

implementation

end.

The use of procedures and functions in modules has its own peculiarities. The subroutine header contains all the information necessary to call it: name, list and type of parameters, result type for functions. This information must be available to other programs and modules. On the other hand, the text of a subroutine that implements its algorithm cannot be used by other programs and modules. Therefore, the headings of procedures and functions are placed in the interface part of the module, and the text is placed in the implementation part.

The interface part of the module contains only visible (accessible to other programs and modules) headers of procedures and functions (without the service word forward). The full text of the procedure or function is placed in the implementation part, and the header may not contain a list of formal parameters.

The source code of the module must be compiled using the Make directive of the Compile submenu and written to disk. The result of module compilation is a file with extension . TPU (Turbo Pascal Unit). The base name of the module is taken from the header of the module.

To connect a module to the program, you must specify its name in the module description section, for example:

uses Crt, Graph;

In the event that the names of variables in the interface part of the module and in the program using this module are the same, the reference will be to the variable described in the program. To refer to a variable declared in a module, you must use a compound name consisting of the module name and the variable name, separated by a dot. The use of compound names applies not only to variable names, but to all names declared in the interface part of the module.

Recursive use of modules is prohibited.

If a module has an initialization section, then the statements in that section will be executed before the program that uses that module starts executing.

Let's list the types of modules.

1. SYSTEM module.

The SYSTEM module implements lower-level support routines for all built-in features such as I/O, string manipulation, floating point operations, and dynamic memory allocation.

The SYSTEM module contains all the standard and built-in Pascal routines and functions. Any Pascal subroutine that is not part of standard Pascal and is not found in any other module is contained in the System module. This unit is automatically used in all programs and does not need to be specified in the uses statement.

2. DOS module.

The Dos module implements numerous Pascal routines and functions that are equivalent to the most commonly used DOS calls, such as GetTime, SetTime, DiskSize, and so on.

3. CRT module.

The CRT module implements a number of powerful programs that provide full control over the PC's features such as screen mode control, extended keyboard codes, colors, windows, and sounds. The CRT module can only be used in programs that run on personal computers IBM PC, PC AT, PS / 2 from IBM and are fully compatible with them.

One of the main advantages of using the CRT module is greater speed and flexibility in screen operations. Programs that do not work with the CRT module display information on the screen using the DOS operating system, which is associated with additional overhead. When using the CRT module, output information is sent directly to the basic input/output system (BIOS) or, for even faster operations, directly to video memory.

4. GRAPH module.

Using the procedures and functions included in this module, you can create various graphics on the screen.

5. OVERLAY module.

The OVERLAY module allows you to reduce the memory requirements of a real mode DOS program. In fact, it is possible to write programs that exceed the total amount of available memory, since only part of the program will be in memory at any given moment.

Author: Tsvetkova A.V.

<< Back: Files (Files. File operations. Modules. Types of modules)

>> Forward: Abstract data structures (Abstract data structures. Stacks. Queues)

We recommend interesting articles Section Lecture notes, cheat sheets:

General psychology. Crib

Banking law. Crib

Hospital Pediatrics. Lecture notes

See other articles Section Lecture notes, cheat sheets.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

The existence of an entropy rule for quantum entanglement has been proven 09.05.2024

Quantum mechanics continues to amaze us with its mysterious phenomena and unexpected discoveries. Recently, Bartosz Regula from the RIKEN Center for Quantum Computing and Ludovico Lamy from the University of Amsterdam presented a new discovery that concerns quantum entanglement and its relation to entropy. Quantum entanglement plays an important role in modern quantum information science and technology. However, the complexity of its structure makes understanding and managing it challenging. Regulus and Lamy's discovery shows that quantum entanglement follows an entropy rule similar to that for classical systems. This discovery opens new perspectives in the field of quantum information science and technology, deepening our understanding of quantum entanglement and its connection to thermodynamics. The results of the study indicate the possibility of reversibility of entanglement transformations, which could greatly simplify their use in various quantum technologies. Opening a new rule ... >>

Mini air conditioner Sony Reon Pocket 5 09.05.2024

Summer is a time for relaxation and travel, but often the heat can turn this time into an unbearable torment. Meet a new product from Sony - the Reon Pocket 5 mini-air conditioner, which promises to make summer more comfortable for its users. Sony has introduced a unique device - the Reon Pocket 5 mini-conditioner, which provides body cooling on hot days. With it, users can enjoy coolness anytime, anywhere by simply wearing it around their neck. This mini air conditioner is equipped with automatic adjustment of operating modes, as well as temperature and humidity sensors. Thanks to innovative technologies, Reon Pocket 5 adjusts its operation depending on the user's activity and environmental conditions. Users can easily adjust the temperature using a dedicated mobile app connected via Bluetooth. Additionally, specially designed T-shirts and shorts are available for convenience, to which a mini air conditioner can be attached. The device can oh ... >>

Energy from space for Starship 08.05.2024

Producing solar energy in space is becoming more feasible with the advent of new technologies and the development of space programs. The head of the startup Virtus Solis shared his vision of using SpaceX's Starship to create orbital power plants capable of powering the Earth. Startup Virtus Solis has unveiled an ambitious project to create orbital power plants using SpaceX's Starship. This idea could significantly change the field of solar energy production, making it more accessible and cheaper. The core of the startup's plan is to reduce the cost of launching satellites into space using Starship. This technological breakthrough is expected to make solar energy production in space more competitive with traditional energy sources. Virtual Solis plans to build large photovoltaic panels in orbit, using Starship to deliver the necessary equipment. However, one of the key challenges ... >>

Random news from the Archive

A new way to control the speed of light 18.04.2019

A group of researchers from the University of Central Florida has found a new way to control the speed of a light pulse. Not only does this method allow you to speed up or slow down the light pulse, it also allows you to reverse the sign of the speed value, i.e. makes the light move in the opposite direction. This achievement in the near future may lead to the emergence of new high-performance optical communication systems, "slow" light pulses can be used as a buffer data storage, which will prevent information loss.

Note that this is far from the first attempt to implement the technology for controlling the speed of light, but in almost all other similar technologies, various materials were used for this, having different refractive indices, light propagation speed and other optical characteristics. The new method is the first to slow down or speed up light in open space without using any material as a light guide.

In their experiments, scientists have shown that they can speed up a pulse to 30 times the normal speed of light, slow it down to half its original speed, and even send that pulse in the opposite direction. Such miracles with light allow them to create a device called a spatial optical modulator. Speaking in simple terms? Without going into physical and mathematical jungle, this modulator allows you to mix the spatial and temporal parameters of a light pulse in various proportions, which in turn allows you to control the speed of this pulse.

“Now we can control the speed of light by directly affecting the impulse itself and reorganizing the energy contained in it. The reorganization of the energy of the impulse manifests itself in mixing its spatial and temporal degrees of freedom,” the researchers write, “While all this is only the first step of extensive future research, the results which could lead to entirely new communications and other optical technologies."

Other interesting news:

▪ Compact device for visualization of brain activity

▪ Local Li-Fi network for the development of the Internet of things

▪ The conductivity of the crystal is increased by 400 times

▪ Liam F1 Silent Wind Turbine

▪ Sex cells grown from stem cells

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ section of the Antenna website. Article selection

▪ article by Aurelius Augustine (Blessed Augustine). Famous aphorisms

▪ article How did Bill Gates benefit from writing a school timetable program? Detailed answer

▪ article Editing director of television and radio programs. Job description

▪ article High-quality sound amplifier for the computer. Encyclopedia of radio electronics and electrical engineering

▪ article Electronic fuse, 6-55 volts 10-600 milliamps. Encyclopedia of radio electronics and electrical engineering

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024