Menu
Is free
check in
the main  /  Advice / What are data files. Data files on computer science and ICT on the topic

What are data files. Data files on computer science and ICT on the topic

Lesson number 7.

Development of a lesson on the topic "Data Files".

Topic of the lesson: data files.

The purpose of the lesson: To form students the concept of data file. File work procedures.

Tasks lesson:

  1. Data files.
  2. File work procedures.

Equipment lesson: Computer, projector.

Lesson plan

1. Organizational moment (greeting).

- Hello, sit down. Who is absent today?

2. Studying a new material.

Data files.

Data file - This is a magnetic medium space reserved for storing information and having a certain name.File - It is also a sequence of numbers (codes), some of which are understood as control codes (for example, there is a code of a sign of the end of the file, for text files there is a sign of the end of the string). What makes this sequence of numbers can be solved only in the program. There are no signs in the file itself to determine the nature of the information.

Working with the file at the physical level is extremely difficult. Therefore, for a file, as for a conventional variable, Pascal defines the type. For example, you can write:

a: File of Integer; (File integers.)

f: File of String; (Line file.)

File working algorithm:

  1. The file is associated with a special procedure with a file variable.
  1. Opened either the file variable is created (the file will be physically opened, but the programmer does not participate in this process).
  2. The necessary read and write operations are performed.
  3. The file variable closes.

Task 1. We will write a program in which the following actions will be performed:

  1. The file opens.
  2. The 100 successive integers are recorded.
  3. The file closes.
  4. The file opens.
  5. The numbers contained in the file are read and printed on the screen.

PROGRAM EXAMPLE;

I, U: Integer;

(The variable is determined, which can then be born to the file,

F: File of Integer;

begin.

(The file variable is attached to the file,

Whose name is indicated in apostrophs)

ASSIGN (F, 'File');

(Since such a file does not exist yet, it is created and opens)

Rewrite (F);

(Numbers are recorded in the file)

For i: \u003d 1 to 100 Do Write (F, I);

(File closes)

Close (F);

(Since the file already exists, it simply opens)

RESET (F);

(Numbers are read from the file and display the display screen)

For i: \u003d 1 to 100 Do

Begin.

Read (F, U);

Write ('', u);

End;

end.

The file variable cannot be a completely ordinary variable. Therefore, special procedures and functions are provided for file variables. A brief list of procedures and functions working with files in Borland Pascal are given later.

  1. assign. - A procedure connecting a file with a file variable.
  2. reset. - Opens an existing file and sets the file position pointer to the zero element.
  3. rewrite - Creates a file.
  4. truncate. - cuts the file starting from the current position.
  5. seek. - Sets the file pointer to the specified position.
  6. eOF. - A function that returns the truth if the end of the file has been reached, and false otherwise.
  7. filesize - Calculates the size of the file in the number of records of the type that is specified in the file declaration.

Note

For the file, there is such a concept as a pointer to the current position. This is the magnitude of the whole type (for Borland Pascal is the value of the typelongint. ), in which the current position of the file is stored. With each read / write operation, the pointer shifts to the next entry. The record is understood as the Length of the type specified in the file declaration. Numbering records in the file starts from zero.

The data structures in the given example are determined correctly, but in the operatorwrite (F, A); The compiler will give an error message. Namely: the compiler will report that there is a mismatch of types. It seems that types of variablesa and F. The same. However, this is not so from the point of view of the compiler. We described two different structures, and the compiler rightly believes that they may be different and does not take care of their verification.

This problem is solved as follows:

PROGRAM EXAMPLE;

Uses CRT;

Type

R \u003d Record.

S: String;

I: integer;

End;

A: R;

F: File of R;

begin.

A.S: \u003d "FSFSFSF";

A.i: \u003d 8;

Write (F, A);

end.

This program implements the same task as the previous one, however, no problems arise for the compiler.

The file can be a component of a complex structure. For example, quite allow an array of files:

f: Array File of Integer;

The file may well be a component of the recording:

PROGRAM EXAMPLE;

A: RECORD.

S: String;

F: File of Integer;

End;

begin.

ASSIGN (A.F, "file.dat"); rewrite (A.F);

end.

This example opens an arrays file. That is, each file entry is an array of 10 integers long. Moreover, we can not define the values \u200b\u200bof all ten elements, in the file it will still be written to them ten, as given in the definition of the array.

The same physical file can be opened as a file of one type, and then it is the same as a file of another type:

PROGRAM EXAMPLE; Uses CRT;

S: String;

I: integer;

F: File of String;

D: File of Integer;

begin.

CLRSCR;

ASSIGN (F, "file.dat"); Rewrite (F);

S: \u003d "GDGDGJAGDJASG";

For i: \u003d 1 to 10 Do Write (F, S);

Close (F);

Assign (D, "file.dat"); reset (D);

While Not Eof (D) Do

Begin.

Read (D, I); Write (I, "");

End;

end.

In this example, the file named file.dat opens as a string and filled with some contents, then closes and opens again, but already as a file number. Such operations for the Pascal language are completely legitimate, this is a consequence of the fact that at the physical level the type of file is not fixed.

Conclusion: File - this structure allows you to store large sets of information. The fact that file types are conventional language data types, adds them convenience in processing. The only lack of data storage in files is a relatively low access speed.

3. Master task.

Repeat the material studied. Learn basic concepts: data file; file; file algorithm; procedures and functions working with files; Record.

4. Summing up

Rate estimates to students who worked well in the lesson.


Data files

In the process of developing programs, it is often necessary to keep storage and processing of saved information. This information may be the most diverse: source data for solving tasks, calculation results, lists, and so on. Database files can be used to store such information. Data files text. Depending on the organization of data on disks or other machine media, text files are divided into files with sequential access, direct access filesand binary files.

Text files with sequential access (serial files)do not have any structure. The structure of these files is determined by the most reading program. In text files with serial access, each line ends with two special characters: the end of the string and return the carriages that are entered into the program text when you press the Enter key (input) on the keyboard. Therefore, one of the easiest ways to process a text file with serial access consists in reading its string string. Creating text files with sequential access also does not represent much work. It can be created by any text editor. The data to the sequential access file is recorded sequential byte byte. To analyze and select the necessary information, the file must be fully read. This improves the requirements for the operational memory and reduces the speed of execution of the program.

Direct Access Text Files (Direct Access Files) Designed to read and write text or structured binary files with fixed-length records. They allow you to record and retrieve data from the file by recording number. This reduces the time to search and extract data. However, there is an ineffective use of disk space, since the length of each field in the record must be specified in advance.

Binary files (binary) are used to read and write arbitrarily structured data. Binary files are strictly speaking, not a new file type, and one of the ways to manage files of any type. Methods for working with binary files allow you to read and modify any byte of the file.

To work with data files, commands of opening a file, closing a file, record and read data from the file, as well as a number of functions that make it easier to work with files. All these commands are traditional for all versions of the Basic language.

Opening files

A team serves to open files Open. .

Open "File Specification" for (File Type)

AS [#] n



Option " Specification_File ", As you know, allows you to specify the disk, route, name and extension of the file name. For example: R: /Prognoz/ucheb/prognoz1.dan. So that the file can be used on computers and with the MS DOS operating system, the file name and its extension should be formed by the rules of the MS DOS operating system. TWO There is only Latin characters and numbers to extend the file name of the file to the file name, the file name must begin with the letter, the file name longer should not exceed 8 characters, and the file name extension is four characters, including the point. The file name is not allowed to use points and spaces. The file specification is quotes.

Option For Defines the type of file. The file type indicates its structure and method of use and can take the following values:

Input - Serial access file, open to read;

Output. - Serial access file, open to record;

Append. - the sequential access file is open to add data;

Bynalary - Binary file is open to record and read data:

Random - Direct access file is open to record and read data.

Option Access. Defines data access rights when working in computer networks. It can have three meanings:

Read. - allowed reading data from the file;

Write - enabled data record to the file;

Read Write. - allowed reading and writing data. This access mode is used by default.

Option Lock . Since read-write mode is usually designed to work with files that can be used by many users or applications, it is necessary to ensure the integrity of data during collective use. For this purpose, the blocking parameter is used, which can take the following values:

Shared. - the file can be used by all processes for reading and writing data;

Lockread. - Prohibition of reading. No other process can read data from the file. This parameter can be set if at the moment no other process performs read operation.

Lockwrite. - Prohibition of recording. No other process can record data into the file. This parameter can be set if at the current time no other process does not perform the record operation.

LockReadwrite. - Prohibition of recording, data reading. This parameter can be installed if at the moment no other process does not perform a record operation reading.

Option As - Specifies the channel number. Sign # You can lower. The channel number may receive values \u200b\u200bfrom 1 to 255. The number of at the same time open channels is determined by the restrictions of the operating system specified in the config.sys file.

Option Len. - Used only in direct access files. It sets the length of recording in bytes.

For openingor, in other words, initialization The following operations are performed:

the connection between the file specification and its software number is established. Therefore, in all subsequent operations with this file, reference is given to the channel number, and not to the file specification;

system or software buffer used to implement I / O statements is fixed. The use of buffer reduces the number of appeals of the program to the disk, and therefore, increases the data read-reading speed;

the initial values \u200b\u200bof the parameters located in the so-called file management unit are generated.

Closing files

To close files, use the command Close . Team syntax:

Close [#<номер канала> ]

The Close command with the channel number option closes the specified channel. The Close command without parameters closes all open files. The Close command clears the buffer and gives the operating system to update the file placement table.

Data recording commands to the file and read information from data files depend on the type of file.

The data in the computer is stored in files. The file is the main structural unit of the organization and storage in the computer. There are quite a few definitions of the concept of a file, the essence of which is reduced to the following: The file has a certain amount of information (the file may contain a computer program, a text document, numeric data, coded pattern, etc.), which has a name and stored on the external memory. The file name consists of two parts, separated by the point: The first part is directly the name of the file, the second is its extension, which defines its type, i.e. what is stored in a specific file (computer program, graphic data, etc.). The file name assigns the user (if the user for some reason does not assign the file name, then the computer program will assign the default name after completing the work), and the file type can be specified forced by a user or an application (computer program) automatically when it is created. In tab. 6.2 Some types of file types and the corresponding extensions, an asterisk designated possible file names.

Table 6.2.

In addition to the file name, there is a concept as a "file format", which defines the information storage method (data structure) in the file and display the file on the screen or when printing. The file format determines the rules for writing file names and depends on the file system supported by the operating system (OS) installed on the computer. For example, in OS MS DOS.the file name must contain no more than eight letters of the Latin alphabet, and the extension consists of three Latin letters - primer.txt, prim.txt, priml.txt, etc. in the family of the family Windowsthe file name can have up to 255 characters, and the Russian alphabet can be used - Album, JPG, example. Suce and so on. These rules in OS MS DOS.and OS family Windowsprohibit when the file names are assigned to use the following signs and symbols: / \\: *?< >|.

On any of the above external memory, a large number of files can be stored. The order of retention is determined by the file system that may have a specific file structure. Each file name corresponds to its unique address, which allows the MP, if necessary, find the desired file on the external memory, and then read the data in RAM from it. Files in operating systems on functional or thematic feature can be combined into directories or folders, which in turn can enter other directories or folders, i.e. have a challenging file structure.

The program of the File Concept is used to solve two tasks:

  • to maintain the results of the program and their further use by other software;
  • using an external data file as a source for input data in the program.

In the Pascal language, external files are considered in terms of the requirements for them with MS-DOS. The file in MS-DOS is called the named area on the disk where the information is stored, the requirement to the file name must strictly comply with the MS-DOC requirements (own name of no more than 8 characters, expansion - 3 characters, letters of the Latin alphabet, the first symbol is required) .

In Pascal, any external file has 3 features:

  • the external file has a name and this allows the program to simultaneously work with multiple data;
  • data files must contain components of the same type;
  • the length of the external file is not negotiated and can have arbitrary length.

External files have a connection with a file variable that replaces the file name in the program. The Pascal file type variable or file type is set in one of the 3 methods in the Type section:

  • [Name]: File of [Type];
  • [Name]: text;
  • [Name]: File;

[Name] - file variable name.

By the declaration method, the following types of files distinguish:

  • typed files (File of);
  • text files (text);
  • not typed files.

The concept of the file can be viewed on both sides: the first side says that the file is the named area on the external memory containing information (data). The file in such a concept is called a physical file, this file exists on the disk physically; The second side says that the file is one of the many data structure that are used in programming. In such an understanding, the file is called a logic file, such a file exists in a logical representation, when drawing up a program. In the program, the file in a logical understanding is represented as a file variable of a certain type.

The structure of the physical file can be represented as a conventional memory byte sequence on the media of the information.

The structure of the logical file is a way to recognize the file program. Figuratively speaking, the logical file is a window (template), with which we look at the physical file structure. In programming languages, this window is the data type used as the File component.

In the logical structure, each component of the external file takes place, which is determined by the type of component. In principle, the logical structure of the data file is similar to the structure of the array. The difference between the file and the array is as follows: the massif, at the time of the memory distribution, the number of elements is recorded, and it is completely in the OP. The numbering of the array elements is performed with the boundaries specified when declaring. The file during operation in the program the number of elements may vary, and, in addition, it is on the outer carrier. The numbering of file elements, except text files begins to the left of the right, starting with 0. The number of components at any time is unknown. But at the end of the file there is a symbol of the end of the EOF file (End of File), which uses a control character from ASCII code # 26 (Ctrl + Z). Also in the program you can determine the length of the file and perform other operations using standard procedures and functions.

A special type of files in Pascal is text files. In principle, the text file can be viewed as a kind of typed files. In text files, in addition to a sign of the end of the file, a sign of the end of the ELN string is still used. An ELN feature corresponds to code # 13, which is generated by the Enter key. The text file structure can be represented as follows:

Standard input files (INPUT) and output (OUTPUT) using keyboard input and display to display are text. Despite the fact that the text file is a variety of char, there are differences in the text file from the symbol file:

  • in text files, when recording, numeric data is automatically converted to the chain of characters, and when reading is performed, there is a reverse transformation;
  • text files do not have direct access, which means that the data can be considered (write) only;
  • read and write a text file can only some standard data data;
  • the text file has signs of the end of the line;
  • to read and write a text file, it is allowed to use ReadLN and Writeln, which are prohibited in other data types.

Procedures and functions for working with external files

To use the file in the program, you must declare a file variable corresponding to the form of a file that will replace the file name in the program. In order for the program to refer to an external file, it must be associated with an external file.

Format: ASSIGN ([File_name], [File__name_Iname_EFI_Enching]);

The file name must contain the full path to this file, starting from the current directory. Two devices are used as a logic device: 'Con' - monitor screen; 'Prn' - Printer.

Rewrite - Creating a new file.

Format: Rewrite ([file_name]);

According to this procedure, an empty, new file with the name of the file variable is created on the media. The new file is prepared for receiving information, and the special variable will indicate the start of the file. If the file with this name existed, it is erased.

Reset. - Set the file to the original state.

Format: Reset ([file_name]);

The procedure prepares an external file associated with the file variable to read. In this case, the file pointer moves to the beginning of the file. The file by the time the Reset must be created.

Read. (Readln.) - read from the file.

Format: Read ([File_named], [List_Vode]);

The names of the variables in the input list should be the same type as the components in the file change file. The value of each variable is set to an equal file component and after reading each value, the pointer moves to the following component. To use the READ and READLN procedures, the file must first be opened by the Reset procedure. ReadLN is used only for text file and when used after reading the next component, the pointer moves to the first symbol of the new line.

Write (Writeln.) - write to the file.

Format: Write ([File_name], [List]);

The value of the variables from the output list is written as file components associated with file variable. Before recording, the file must be created by the Rewrite procedure. Writeln After recording the next component, moves a pointer to the beginning of a new line.

When working with text files, you must properly follow the use of Read (ReadLN) and WRITE (Writeln) procedures. When writing WRITE data is read read, when writing Writeln is reading readln.

Close - Closing the file.

Format: Close ([file_name]);

The procedures closes the file, but the connection to the file variable with the file installed earlier in the ASSIGN is preserved. Since the file link and file varia can be opened for recording or reading without additional use of ASSIGN.

Append. - Adding elements.

Format: Append ([file_name]);

The procedure opens an existing file to add data to the end of the file. If the file has already been open, the use of Appende will close and open it again to add a record.

Erase. - erases the file from the disk.

Format: ERASE ([file_named]);

Erasing a file associated with file varia. Before using the procedure, the file must close Close.

EF function

Format: Eof ([file_name]);

A logical function returns truth if the pointer at the end of the file and a lie else.

ELN function

Format: Eoln ([file_name]);

The logical function returns the truth if the pointer at the end of the line and lies in another case.

All information contained in variables and arrays disappears at the end of the program. If it may be needed in the future, then this information is saved on a hard disk, a diskette or a different carrier, and the next time you start the program, the program is back from the outer medium in arrays or variables. Such an entry on a disk or other carrier is called data file. In addition to data files, there are also executable files (programs in machine codes with extension.exe or.com). Some languages \u200b\u200band programming systems (including from Pascal - Borland Pascal and Delphi dialects) allow you to start such a file from your own program. Pascal ABC does not have such an opportunity.

Files are combined into groups (directories or folders). To determine how the file has to work, indicate it full name. It consists of several parts, for example:

The catalog in which our program is located is a worker (current) directory. This denotes that when accessing the data file, which is in such a directory, specify the path is not necessary.

To use the data file in the program you need in the VAR section describe a file variable (F / P), in which information will be stored about this file (full name, length, date and time of creation, etc.). This description is different for different types of files. For example:


In the files described as typical, all data have the same type (specified when describing), as in arrays. Therefore, in such files, the contents of arrays usually retain. Store the text in the type file is inconvenient, as all lines must be the same length. To store text with strings of different lengths, a text file is used. To separate one line from another, in such a file, each line ends with the symbol corresponding to the Enter key. In addition to rows in a text file, you can store and numbers (but it is less economical than in the standard file, where they are stored in binary code, and not prevail).

To put the file name to the file variable, Used command

aSSIGN (F / P, 'full file name');

In this command, you can not specify the path for the file located in the working directory (there is where the program itself is).

To the file variable gets all the other file information, it is necessary open Using one of the following commands:

rESET (F / P); - Opens an existing file and sets the pointer to its beginning (usually you can write a file for reading, but you can record the type file. The recording occurs instead of the data file available).

append (F / P); - Opens an existing file and sets the pointer to its end (to add information after the end of the file).

rewrite (F / P); - Creates a new file (if the file already exists, it is cleared).

The file pointer stores a place in the file, from where we can read (or where to write) information. Each read or recording operation moves the pointer to the next record.

To read information from fileBy placing it in a variable, commands are used:

read (F / P, list of variables); - reads from a typical and text file.

rEADLN (F / P, list of variables); - reads from a text file.

If not separate words or numbers are read from the text file, but the whole line, then only one row variable is used.

wRITE (F / P, list of variables); - writes to a typical and text file.

writeln (F / P, list of variables); - writes to a text file with a row translation.

To move the file (only to the Type!) Overhead, the SEEK command is used (F / P, recording number); . Numbering entries in the file is kept from scratch. To get to the end of the file to add a new entry, as a number in this command, the Filesize feature (F / P), which defines the file size (not in bytes, and in the records of this type).

When reading an unknown length from the file, you can check if you check if another file ended using the EF function (F / P), which gives True if the pointer is after the last entry. For example:


At the end of working with the file it is necessary close command Close (F / P); . If you do not do this, Pascal will not save the file change on the disk. But if there were no changes, it is recommended to close the file. After that, the file variable and file buffers (the memory area that Pascal uses when working with memory) is released.

The RENAME command (F / P, 'New_MIAM') renames a file associated with a file variable. The file must be closed.

The ERASE command (F / P) deletes a file associated with the file variable. The file must be closed.

Not always file operations are successful. For example, when trying to read from a diskette that you forgot to insert into the drive, an error message appears, and an emergency completion of the program occurs. Similar errors are better to warn. FileExists feature ('name') Returns true if the file has a file with this name (and by way), otherwise returns False. It must be used before trying to appeal to an existing file. If you need to create a new file, the CancreateFile ('name') function is used before it. If the file name or path are incorrect, or there is no place on the disk, or for other reasons it is not possible to create a file with the same name, it returns False.