Files with the txt extension are text files. Abstract: Text files. Text file editors. Procedures for opening text files

TOPIC #10:FILES. TYPED FILES. TEXT FILES.

Basic Concepts

Files is a named data structure on a technical medium, which is a sequence of elements (records) of the same type.

Files is a variable-length array of unlimited size.

The file can be part of another complex structure, but must not be part of another file.

Difference between a file and an array:

A) placement on external media;

B) the file length is not specified;

C) the location of the element is not determined by the index.

To designate a file and work with it it is used file variable(FP).

File variable is a variable used in programs to designate and access a file.


File Variable Features:

It cannot be assigned any values.

It cannot participate in logical operations

It cannot be included in mathematical expressions

File Variable Declaration

The file is declared in the variable description section indicating the type of file elements.

File type

File Description

Typed

Var F1: file of integer;

F2: file of char;

Untyped

Text

WORKING WITH A FILE

A) FILE RECORD

Assign(FP, 'Name');

Opening a file for writing

Writing data to a file

Write(FP, Data);

Writeln(FP, Data); - for text

Closing the file

B) READING FROM FILE

We associate a file (possibly non-existent) with the name Name with a file variable (FP)

Assign(FP, 'Name');

Open the file for reading (the pointer is set to the first element)

We read data from the file into variables and move the file pointer forward.

Read(FP, Variables);

Readln(FP, Variables); - for text

Closing the file

When working with a file, the concept of a pointer or file index is used, i.e. the position of the magnetic head in a certain place in the file.

DETERMINING THE LENGTH OF TEXT FILES

There is no command in Pascal that would determine the number of lines written to a text file. However, there is a function that tells you when the file pointer has reached last line - EOF (End Of File)

EOF(FP) – confirming function

reaching the end of the text file

If the end of the file is reached, then EOF is true.

If the end of the file is not reached, then EOF is false.

An example of reading a text file that contains an unknown number of lines. At the end of the program we will display the number of lines on the screen.

Var f:text; N:integer; s:string;

Assign(f, 'c:\MyText. txt');

While Eof(f)=false do ( You can write this line shorter: While not Eof(f) do}

Writeln("In file c:\MyText.txt", N, "lines");

DETERMINING THE LENGTH OF A BINARY FILE

For binary typed files, the method used when reading text files. The EOF function works the same for both text and binary files.

Additionally, when working with binary files, you can use the FileSize function, which determines the length of the binary file.

FileSize (FP) – function returning the length of a binary file

A program that reads all the numbers from a binary integer file and finds their sum:

Var f: file of integer;

Summ, i, T:integer;

Assign(f, 'c:\MyFile.int');

For i:=1 to Filesize(f) do ( You can use another loop: While not Eof(f) do}

Writeln("The sum of the numbers in the file c:\MyFile. int is ", Summ);

Writeln("Length of file c:\MyFile. int", FileSize(f), "numbers");

NAVIGATING THROUGH A BINARY FILE

For binary files, there is a Seek procedure that moves the read and write pointer to the desired position in the file.

SEEK (FP, Position) – moves the pointer

to a given file position

A program that resets a number at a given location in a binary real file and appends this number to the end of the file.

Var f: file of real; (we declare a file variable for a real file)

N:= FileSize(f); (let's determine the number of numbers in the file)

Repeat (start of cycle)

Writeln("In the file ‘, N, ‘ numbers’);

Writeln("Enter the number of the number that should be moved to the end of the file?’);

Until (Poz>=0) and (Poz<=N) {выход из цикла, только если в данном файле такая позиция существует}

Seek(f, Poz); (position the file pointer to a given position)

Read(f, R); (we read the number from the given file position into the variable R, the pointer moves one position forward)

Seek(f, Poz); (return the pointer to the specified position)

Write(f, Null); (we reset the number in which the pointer is set)

Seek(f, N); (move the pointer to the end of the file)

Write(f, R); (we write the number from the variable R to the end of the file)

REDUCING BINARY FILES

To shorten a binary file, you need to set the file pointer to the place beyond which the unnecessary information is located and cut off the unnecessary part using the Truncate procedure.

Truncate (FP ) procedure that truncates data located after the file pointer

A program that leaves only the first 10 numbers in a binary file:

Var f: file of real; (declare a file variable)

Assign(f, 'c:\MyRealFile. rlf'); (associate a file variable with a file)

Reset(f); (open the file for reading and writing)

Seek(f, 10); (position the file pointer to a given position)

Truncate(f); (we cut off from the file everything that is located further than the file pointer)

ADDING INFORMATION TO TEXT FILES

Unfortunately, when working with text files, there is no way to insert any information into the middle or at the beginning of the file. The only possible change to the file is the ability to add lines to the end of the file. To do this, the file must be opened for adding.

Append(FP)– a procedure that opens a text file

to add lines

A program that appends two lines to the end of a text file:

Var FT: text; s:string;

Assign(FT, 'q.txt');

S:=’First added line’; Writeln(FT, S);

S:=’Second added line’; Writeln(FT, S);

RENAME, TRANSFER FILE

When working with placing information on a disk, you should be aware that when transferring information from one folder (as opposed to copying, where a duplicate of the data is created) to another, there is no actual transfer of data. When transferring information from one folder to another, only the path in the file changes, that is, in fact, only the name of the file changes.

Therefore, in Pascal, the same command performs both renaming a file and moving a file between directories.

ReName (FP, New_file_name) a procedure that renames a file and moves it to a directory,

specified in the New file name.

The Rename command only works on closed (or not yet opened) files. Therefore, before using it, you do not need to open the file to read, write or add data. Or, if the file is already open, you should first close it.

The program must give the file 'q1.txt' a new name, which the user will enter, and transfer the file 'q2. txt’ to catalog ‘c:\ Document\’.

Var FT: text; s:string;

Assign(FT, 'q1.txt'); (associate a file variable with a file)

Writeln('Enter a new name for the file q1.txt');

Readln(s); (we read the new file name into the s variable from the keyboard)

Rename(FT, s); (rename the file)

Assign(FT, 'q2.txt'); (we associate the file variable with the second file)

Rename(FT, 'c:\Document\q2.txt'); (move the file to the specified directory with the same name)

To transfer a file to another drive, you need to copy the file and then delete it in its original location

COPYING AND DELETING A FILE

Copy: To copy a file, you need to read all the records of one file and write them to the newly created file.

Removal: To delete a file, simply apply the Erase procedure to the file variable (associated with the closed file).

ERASE (FP) a procedure that deletes a file associated with a file variable.

The program copies the file from drive "C" to diskcTo 'D'and deletes the original from drive "C" (in fact, the program transfers the file from one drive to another)

Var f1, f2: file of byte;

Assign(f1, 'c:\MyFile.int'); Reset(f1); (open the sample file for reading)

Assign(f2, 'd:\MyFile.int'); Rewrite(f2); (create a file on drive D)

While not Eof(f1) do

Read(f1, b); (we read data from the original file)

Write(f2, b); (we write the data to a new file)

Close(f1); (close the sample file, the file must be deleted before deleting)

Close(f2); (close new file)

Erase (f1); (delete the file)

WORKING WITH CATALOGS

MkDir (Directory Name) – Procedure creates a new one catalog

(If a directory with the same name exists, an error will occur)

RmDir(Directory name) – procedure deletes catalog

(Only an empty directory can be deleted)

ChDir (Directory Name) – procedure changes current catalog

(The current directory is a directory whose path does not need to be specified when working with files. When you turn on a program, the current directory is the directory in which the program is located.)

For renaming catalog necessary :

a) Create a new directory

b) Move all files there

c) Delete the old directory

The program renames the directory “c:\Cat\”, in which two files “q1.txt” and “q2.ini” are located, into the directory “c:\CatNew\’ and makes the directory current.

Var f1,f2: file of byte; b:byte;

MkDir("c:\CatNew"); (create a new directory)

(copy the q1.txt file to the new directory)

Assign(f1, 'c:\Cat\q1.txt'); Reset(f1);

Assign(f2, 'c:\CatNew\q1.txt'); Rewrite(f2);

While not Eof(f1) do

Begin Read(f1, b); Write(f2, b); End;

(copy the q2.ini file to the new directory)

Assign(f1, 'c:\Cat\q2.ini'); Reset(f1);

Assign(f2, 'c:\CatNew\q2.ini'); Rewrite(f2);

While not Eof(f1) do

Begin Read(f1, b); Write(f2, b); End;

Close(f1); Close(f2); Erase (f1);

RmDir('c:\Cat'); (deleting the old directory)

ChDir('c:\CatNew'); (make the new directory current)

Search files

The programmer does not always know which files are located in which directories and which directories are located on which disks. Therefore, an important topic in file manipulation is finding files (and directories).

To search for files, there are two procedures located in the module WinDos:

FindFirst(Mask, Flag, Information Receiver)– searches for the first file that matches the “mask” and “flag”; if found, transfers data about the found file (directory) to a variable like TSearchRec(“information receiver”)

FindNext (Information_receiver)– searches for the next file that meets the conditions set in the FindFirst procedure, if found, transfers data about the found file (directory) to a variable like TSearchRec(“information receiver”)

Module WinDos also contains a variable DosError, which remains zero as long as the search commands find a file that matches the search condition. If the search commands did not find files matching the condition, then the variable

Search mask – a string containing the name of the file or directory to be searched. In addition to regular characters, the mask can contain “*” and “?”.

* - means that in the desired file at this location there may be any number of any characters;

? – means that any one character can be present in the searched file at this location.

Examples of masks:

"*.* ’ – any file with any extension

"quest.*" – file with the name "quest" and any extension

"*.txt" – file with any name and extension "txt"

"qust?.txt" – a file with the extension ‘txt’, containing 5 letters in the name, the first 4 of which are ‘qust’, and the 5th character is any

"qust*.txt" - a file with the extension 'txt', the first four letters of which are 'qust', the number of subsequent letters is not limited

"quest. txt’ – file with the name “quest” and extension “txt”

Search flag - a set of special constants connected by the OR operator.

Instead of constants, you can use a number equal to the sum of numbers with the corresponding constants

Flag

File type

Number

faReadOnly

Search with Read-Only attribute

faHidden

Search with the “Hidden” attribute

faSysFile

Search with the attribute "System"

faVolumeID

Return media id

faDirectory

Return directory information

faArchive

Search with the “Archive” attribute

faAnyFile

Enable all previous flags

Examples of flags:

· faReadOnly or FaHidden – 3 - searches for files that have the “hidden” or “read-only” attribute

· faDirectoty orFaSysFile - 20 – searches for directories and files that have the “System” attribute set

Information receiver – type variable TSearchRec, which will contain data about the found file

The TSearchRec type is described in the WinDos module and does not require prior description by the programmer. This type is a record of the following form:

Type TSearchRec = Record

Fill: Array of byte; (System parameter, not used by us)

Attr: Byte; (Parameter containing file flag data)

Time: LongInt; (File creation time)

Size: LongInt; (File size in bytes)

Name: Array of Char; (File name)

Let's say we have a variable SR, which is of type TSearchRec. After using it in the FindFirst command, it will contain information about the found file (directory.)

Information about the file name will end up in SR. Name, about the file size in SR. Size, and to determine whether the file is system, you need to connect the SR value. Attr with the FaSysFile constant and the And operator. The file is system if the result is greater than zero.

Example :

IF Sr. Attr and FaSysFile > 0 THEN Writeln('System');

The program searches the "c:\Doc" directory for all files with the "txt" extension. And displays the name of those whose size is more than 3000 bytes.

Uses Dos, crt, WinDos;

Var SR: TSearchRec; (We describe a variable in which we will store information about found files)

clrscr; ChDir("c:\Doc"); (Change the current directory)

FindFirst("*.txt",faAnyFile, SR); (We look for the first file in the current directory with the extension “txt’ and any set of flags, the result is stored in the SR variable)

while DosError=0 do (run the loop while the files are located)

if SR. Size > 3000 (if the file size is more than 3000 bytes)

then writeln(SR. Name); (Display the name of the found file on the screen)

The program searches for all directories located on the disk in the root directory of drive “C” and displays a list of directories in the file “c:\info. txt’ and to the screen (The root directory of the “C” drive is the “c:\” directory).

Uses Dos, crt, WinDos;

Var SR: TSearchRec; f:text;

Clrscr; Assign (f, "c:\info. txt"); Rewrite(f);

ChDir("c:\"); (Go to the root directory)

FindFirst("*",faDirectory, SR); (We are looking for a file without extensions including directories)

While DosError=0 do(we run the loop while the files are located)

if SR. Attr and faDirectory>0 then (if the file has the "Directory" attribute)

Writeln(SR.Name); (display the file name on the screen)

Writeln(f, Sr. Name); (output the file name to the file "c:\info. txt")

FindNext(SR); (Looking for the next file)

Procedures for working with file variables

Procedure

Purpose

Comment

Assign(f, 'name')

Binds a file variable to a specific file (binds the variable to the file name). Placed before the first use of the file variable.

You can set the file name 'name' through a string variable (S): Assign(f, S)

Reset(f)

Opens an existing file and sets the pointer to the beginning of the entry.

For any files

Rewrite(f)

Creates a new empty file, sets a pointer to the beginning of the file. If the file existed, its contents are destroyed.

For any files

Close(f)

Closes open file

For any files

Erase(f)

Erases a previously closed file

For any files

Rename(f, f1)

Renames file f to file f1. File f must be previously closed

For any files

Read(f, v1,…vn)

Write(f, v1,…vn)

Writes the values ​​of variables v1,…vn to file f

For typed and untyped files

Readln(f, v1,..vn)

Reads entries from file f into variables v1, ..vn

Writeln(f, v1,.vn)

Writes the values ​​of variables v1,..vn to file f

not allowed in typed files

Append(f)

Opens a file and sets the pointer to the end-of-file mark

Used to add entries

seek(f, n)

Sets a pointer to record number n

truncate(f)

Trims all records after the pointer and writes an end-of-file mark at this position.

Functions when working with file variables

Procedure

Purpose

Comment

Getting information about the end of the file True if the pointer is at the end of the file, otherwise - False

Logic function.

True if the pointer is at the end of line mark, otherwise False

Logic function. For text files

N:=filesize(F);

Determining the number of records in a file

N:=filepos(f)

Returns the number of the record pointed to by the record pointer. The first entry is number 0.

Example programs

TASK 1: Write a programwrites data about the author and 20 random numbers into a text file

randomize; (setting up a random number generator)

rewrite(f); (open the file for writing)

r:=random(10); (we generate a random number from 1 to 10)

STR(r, s); (convert the number r into the string s)

writeln(f, s); (write a string with a number to a file)

close(f); (close the file)

TASK 2: Write a programreading data about the author and 20 random numbers from a text file

r, i,c: integer;

assign(f,"dddddd. txt"); (we associate the file variable with the file)

reset(f); (open the file for reading)

for i:=1 to 20 do (include a cycle of 20 repetitions)

readln(f, s); (read the number from the file into line s)

val(s, r, c); (translate the string “s” into the number “r”, in the variable “c” the possible error code)

if c=0 then writeln(r); (if there was no error (c=0), then we display the number on the screen)

close(f); (close the file)

TASK 3: Calculate the roots of a quadratic equation and output the results to a file.

Var a, b,c, x1,x2, d: real;

Assign(f,’result. txt); (we associate the file with the file variable f)

Rewrite(f); (open the file for writing)

(We read the source data from the keyboard and send it to a file)

Writeln(f, ‘solution to square equation’);

Writeln(f, "a=",a:6:3,"b=", b:6:3,"c=",c:6:3);

(Calculate the roots of a quadratic equation)

If d>=0 then Begin X1:=-b+sqrt(d)/(2*a); X2:=-b-sqrt(d)/(2*a); End;

(Output the result to a file)

If d<0 then writeln(f,’у урав-я нет корней’)

else writeln(f, ‘roots: x1=’, x1:6:3,’x2=’, x2:6:3);

(Close the file and end the program)

Writeln('results of the program in the file result. txt');

There is a file on the disk named Karl. txt, which contains the tongue twister: “Karl stole corals from Clara, Clara stole Karl’s clarinet.” You need to count the number of letters “K”

Var f:text; s:integer; a:char;

(We associate the file with a file variable and open it for writing)

Assign(f,’Karl. txt’); Reset(f);

(consecutively look through all lines of the file until the last)

While not eof(f) do

(we sequentially read all the characters of the string until the last one in the string and compare it with “k”)

While not eoln(f) do

Begin Read(f, a); If a=’k’ then s:=s+1; End;

(go to the next line of the file)

Enter text into a text file f1. Rewrite a line from file f1 to f2 - the first half of the file in forward, and the second in reverse character order.

Var f1,f2:text; s, snew, sa, sb: string; n, y,i: integer;

(Associate files with file variables)

Assign(f1,’file1’); Assign(f2,’file2’);

(We read a line from the keyboard and send it to file No. 1)

Rewrite(f1); Readln(s); Writeln(f1,s); Close(f1);

(Reading a line from file No. 1)

Reset(f1); Read(f1,s1); Close(f1);

(Find the middle of the line)

N:=length(s1); Y:=n div 2;

(In the new variable we write the first half of the line in forward order, the second half in reverse order)

For i:=1 to y do

Snew:=Snew+s1[i];

For i:=n downto y+1 do

Snew:=sew+s1[i];

(Write down new line to file No. 2)

Rewrite(f2); Writeln(sNew); Write(f2,sNew); Close(f2);

The set of rules by which data is stored in a file is called a file format. Different types of files, such as text files, raster graphics, etc., use different formats. In general, several different formats can be defined for the same file type, although file type and format often mean the same thing. A file format is identified by the file name extension that is added to the file name when it is saved in a specific format, such as DOC, GIF, etc.

Typically, file formats are created for use in a strictly defined application program. For example, graphic objects created in the well-known vector graphics package CorelDRAW are saved as files with the CDR extension, and images generated by another graphics package, CorelXara, are written to disk as files with the XAR extension. Some formats are not associated with specific applications, that is, they are universal. One of the most well-known universal formats is the TXT format (DOS text file format).

Compression of computer files is often used to save storage space. There are many ways to compress files. These methods depend on the source file format. In general, the higher the compression ratio, the slower read and write operations are.

As for compression algorithms, there are both compression algorithms without data loss and algorithms that may result in data loss.

Lossless compression ensures that all the data that was in the file before compression will be present after the file is decompressed. Lossless compression mechanisms are used when storing text or numeric data, such as spreadsheets or document files. Examples of lossless compression algorithms include the well-known algorithms ZIP, ARJ, and others.

Let's give a brief description of the main formats used:

§ American Standard Code for Information Interchange ASCII (TXT). Text file format developed by the American National Standards Institute. Supported by all operating systems and all programs. It is a text file in DOS encoding, there is no function to insert a picture, there is no formatting, it works on all machines, it is possible to create only small files.



§ ANSI (TXT). ANSI text file format (for Microsoft Windows code page)

§ MsWord for DOS, Windows (.DOC). The document format, developed by Microsoft Corporation, is supported by MS-DOS programs and most word processors. It preserves the original formatting of documents, as well as character styles. In addition to text information, files in this format can contain graphic images with various parameters. Supports 256 colors. Does not support compression. Used primarily for exchanging formatted text data between different platforms and applications.

§ Hypertext Markup Language HTML (HTM, HTML). Markup language for hypertext documents. All pages located on the Internet are created using this special language. HTML documents are ASCII files that can be viewed and edited in any text editor. The difference from a regular text file is that HTML documents contain special tag commands that define the document formatting rule. If you manage to master the HTML language, then you can create pages for the Internet. By adding tags (labels) to plain text, you force the viewer to display that text in a certain way and place images on the page. If you've learned Java and JavaScript, you know how to extend HTML by placing commands written in a scripting language inside tags.

§ Portable Document Format PDF (.PDF). This document storage format, developed by Adobe, claims to be an open typographic standard for the Web. It is seen as an alternative to HTML. The disadvantage of HTML is that documents translated into HTML usually do not retain the original format, and HTML offers a very limited number of fonts when viewed. In contrast, users of Acrobat and PDF tools for creating, sharing, and viewing documents in their original format know that readers will see the publication exactly as it was created. The PDF format is indispensable if you need to get an exact copy of the required document. As an example of the successful use of PDF for documents in Russian, we will give the Moscow News server on the Internet. The materials presented on it in electronic form completely replicate the printed paper original.

§ Standard Generalized Markup Language (SGML). The development of HTML translates into a standard generalized markup language. It is a toolkit of mechanisms for creating structured documents marked using descriptors (tags). Compared to HTML, it provides more flexible and versatile formatting options on the Web. However, SGML also has increased speed, so PDF is used as a simpler tool. The power of SGML lies in its cross-platform structured approach to describing the content of documents. SGML is actually a metalanguage, i.e. is intended to describe markup languages ​​used to create documents.

Text files consist of character strings. Lines can have different lengths, and each line has a line terminator at the end. To describe text files, the service word Text is used:

Var A: Text;

To process text files, the same procedures and functions are used as to process regular typed files. To associate a file variable with a file on disk, use the Assign procedure. Text files can be opened for reading with the Reset procedure or for writing with the Rewrite procedure.

The Read procedure is used to read data. If it is necessary to move to the next line after reading data, then the Readln procedure is used. If you just need to move to the next line, you can use the Readln(<>); which sets the file pointer to the first element of the next line.

The Write procedure writes data to the current row. If you need to write data and move to the next line, you can use the Writeln procedure. If you only need to go to a new line to write, then the Writeln(<имя файловой переменной текстового файла>); which writes a line terminator to the file and sets the file pointer to the beginning of the next line.

Since strings can have different numbers of characters, there is a Boolean function Eoln(< file variable name of the text file >) , which evaluates to True if the end of the line is reached.

Procedure Append(< file variable name of the text file >). It opens the file for "append", placing the file pointer at the end of the file.

Example: Given a text file containing only integers, each line can contain several numbers that are separated by spaces. Display all the numbers taking into account the division into lines and count the number of elements in each line.

Solution: Let the file contain the following information:

4 5 9 13 11 -5 -8

This file can be created in the Turbo Pascal environment as follows:

* create a new file using the command New menu File;

* write down all the numbers, separating them with spaces, and break them into lines as indicated in the task;

* save the file, for example, under the name INT1.DAT

* now let's write a program

program rrr;

var f:text;

Assign(f,’int1.dat’);

While Not Eof(f) do (until the end of the file is reached)

While Not Eoln(f) do (until the end of the line is reached)

read(f,x); (read the next number)

write(x,’ ‘); (we display it on the screen)


Inc(k); (increase the counter)

writeln('in line', k, 'elements');

Readln(f) (go to next line of file)

Example. Write a two-dimensional 5x4 array of real numbers to a test file.

Program mas;

const m=5; n=4;

Var fil:text;

Assign(fil,'massiv.txt');

for i:=1 to m do

for j:=1 to n do

write(fil,a:5:3,’ ‘); (the number is written to the file in the specified format, followed by a space)

writeln(fil); (go to a new line in the file)

(Reading a file and displaying the matrix line by line)

Reset(fil); (opening an existing file)

while not Eof(fil) do

while not Eoln(fil) do

read(fil,a); (reading the number)

read(fil,s); (reading space after number)

Example. Given a text file f. Copy all components to file g source file f in reverse order.

program tofile;

var f, g: text;

n, i, j: integer;

x: array of char;

assign(f,'f.txt'); assign(g,’g.txt’);

rewrite(g); rewrite(f);

writeln('Enter the number of lines in the file you are creating');

writeln(‘enter lines, after entering each line, press Enter’);

for i:=1 to n do begin readln(s); write(f,s); end;

writeln('Source file:');

while(not eof(f)) and (i<32000) do

begin i:=i+1; read(f,x[i]); write(x[i]); end;

writeln('Changed file:');

for j:=i downto 1 do

begin write(g,x[j]); write(x[j]); end;

close(f); close(g);

Task. Given a text file. Insert its number at the beginning of each line and write the converted lines to a new file.

Task. Two text files are given. Write into the third file only those lines that are in both the first and second files.

INTRODUCTION

Almost every computer user encounters the need to prepare certain documents - letters, articles, memos, reports, advertising materials, etc. Of course, these documents can be prepared without a computer, for example on a typewriter. However, with the advent of personal computers, it has become much easier and more convenient, and therefore more profitable, to prepare documents using computers.

When using personal computers to prepare documents, the text of the document being edited is displayed on the screen, and the user can make changes to it online. All changes made are immediately displayed on the computer screen, and then when printed, beautiful and correctly formatted text is displayed, which takes into account all the corrections made by the user. The user can transfer pieces of text from one place in the document to another, use several types of fonts to highlight individual sections of text, and print the prepared document on a printer in the required number of copies.

The convenience and efficiency of using computers for preparing texts has led to the creation of many programs for document processing. Such programs are called text editors(Word Processors). The capabilities of these programs are varied - from programs designed for preparing small documents of a simple structure, to programs for typing, design and complete preparation for printing of books and magazines (publishing systems).


Before you start exploring the MS-DOS Editor menu, you should practice typing. The text is typed from the keyboard as on a regular typewriter; at the end of each line, Enter is pressed.

To split a line that is too long into two, press Enter where the end of the line should be.

Each press of Enter adds a blank line. If excess are formed empty lines, delete them you can press Shay Del.

You can correct errors in the text by moving the cursor across the working field using the keys or the mouse. To delete a character, use the Del key if the cursor is before the character you want to delete, or the Backspace key if the cursor is after the character.

If you need to delete a character only to type another in its place, it is more convenient to switch the keyboard to replacement mode. By default, the keyboard is in insert mode. The Ins key switches between insert and replace modes.

When inserted, all subsequent characters are shifted to the right.

When replaced, the current character disappears.

Documents created in the MS-DOS Editor can be saved in text files; to do this, use the menu File Save. The File Save As... menu will allow you to save the file under a different name.

To clear the editor and start working on a new file, use the menu File New. To load a ready-made file into the editor, use the menu File Open. In the dialog panel, select the name of the required file with the cursor. Menu File Print allows you to print either a selected part of a document or the entire text.

Among simple text editors in Russia, LEXICON is most widely used.

Word processor Lexicon

Lexicon word processor developed E.N.Veselov in 1985 at the Computing Center of the USSR Academy of Sciences. Since 1991 supplied by the company Microinform. It has an interface in Russian and allows you to prepare simple documents with text in Russian and English. LEXICON successfully fills its “ecological niche” - it is quite suitable for those who need a simple tool for preparing small and uncomplicated documents, and they do not require high printing quality.

To start working on a new file, you need to give the menu command Clear text or use any free Lexicon window. There are a total of 10 windows available, and by pressing A + "qi fra on the alphanumeric keyboard, you can go to the window with the corresponding number.

To load a ready-made file, use the menu command Download text and select the name of the required file in the menu with the cursor.

Menu command Print text t Start will allow you to print a document on a printer if the PRINTER1FILE 1SCREEN switch is set to the PRINTER position. When in the SCREEN position, you can see exactly how the text printed on the printer will look.

Editor MS-Word

Here are just some of the features supported by Word:

· use of many different fonts (sizes and styles) of characters and different ways of highlighting them (bold, italic, underlined characters, etc.); specifying the parameters of text paragraphs and document pages; typing text in several columns; printing headers and footers of any type; automatic generation of a table of contents and various types of indexes;

· design of tables and paragraphs “side by side”; inclusion of drawings (graphic files); placing paragraphs (for example, pictures) anywhere on the page (the rest of the text can “bend around” the picture).

Experienced users really appreciate Word's style features. Word allows you to record in the so-called style sheet all the parameters of the most commonly used types of text formatting: paragraphs, characters and document sections. If you do this, then any part of the text can be assigned one of the “standard” design types using one or two keystrokes. This not only significantly speeds up document typing, but also increases the flexibility of its design. For example, to change the font and location of all headings of a certain level (say, paragraph headings), you do not need to search for these headings and manually change their formatting - just correct the style for these headings, and they will automatically take the desired design.

Shift+Ctri with "M""1", "N""WITH", «(» and "5" (on the right side of the keyboard) are reserved.

In fact, there are many more “forbidden” combinations. If you define combinations with Shift for your programs, then from time to time the user will completely unexpectedly “fall out” of the text editor when trying to write a capital letter, and combinations with Alt will not be in vain when working in programs where menu options are called up by Alt+letter- just like in MS-DOS Shell itself. In Microsoft Word, almost all possible key combinations are reserved for internal needs!

The task switch itself is a DOSSWAP.EXE program that loads before any application program is executed and exits after the program exits, returning to MS-DOS Shell. The DOSSWAP program takes up about 30 KB of RAM.

Text files

Text files are designed to store text information. It is in such files that, for example, the source codes of programs are stored. Text file components can have variable length, which significantly affects how you work with them. Each line of a Pascal text file can only be accessed sequentially, starting from the first. The procedures assign, reset, rewrite, read, write and the eof function are applicable to text files. When creating a text file, a special sign EOLN (end of line) is placed at the end of each record (line). To determine whether the end of a line has been reached, there is a logical function of the same name EOLN(<имя_ф_переменной>), which evaluates to true if the end of the string is reached.

In addition to the read and write procedures, when working with text files, their varieties readln and writeln are used. The difference is that the writeln procedure, after writing the given list, writes a special end-of-line marker to the file. This sign is perceived as a transition to a new line. The readln procedure, after reading a given list, looks for the next line terminator in the file and prepares to read from the beginning of the next line.

An example of solving a problem with files

Suppose we need to create a text file, and then copy from this file into the second only those lines that begin with the letter “A” or “a”.

Solution: we will need two file variables f1 and f2, since both files are text, the variable type will be text. The task is divided into two stages: the first is the formation of the first file; the second is reading the first file and generating the second, then displaying the contents of the second file on the screen.

Program primer;

Var f1,f2:text;

I,n: integer;

S:string;

Begin

(form the first file)

Assign(f1, 'file1.txt'); (we establish a connection between a file variable and a physical file on disk)

Rewrite(f1); (open the file for writing)

Readln(n) (determine the number of lines to be entered)

for i:=1 to n do

begin

readln(s); (enter strings from the keyboard)

writeln(f1,s); (we write sequential lines to the file)

end;

close(f1); (we finish working with the first file, now there is a file on the disk named file1.txt containing the lines we entered. We can finish the program here, we can continue working with the file in another program, at another time, but we will continue)

(part two: reading from the first file and forming the second)

Reset(f1); (open the first file for reading)

Assign(f2, 'file2.txt'); (we establish a connection between the second file variable and the physical file)

Rewrite(f2); (open the second file for writing)

(Next, you need to sequentially read lines from the first file, check whether the condition is met, and write the required lines to the second file. To read from a text file, it is recommended to use a loop according to the condition “until the end of the file”)

While not eof(f1) do

Begin

Readln(f1,s);(read the next line from the first file)

If (s=’A’) or (s=’a’) then

Writeln(f2,s); (we write lines that satisfy the condition into the second file)

End;

Close(f1,f2); (we finish working with the files)

(part three: display the second file)

Writeln;

Writeln('The second file contains the lines:');

Reset(f2); (open the second file for reading)

While not eof(f2) do (until the end of the second file)

Begin

Readln(f2,s);(read the next line from the second file)

Writeln(s); (display the line on the screen)

End;

End.

Task 1: Given a text file. Count the number of lines in a file.

  1. Open file for reading;
  2. Organize reading data from a file line by line (readln(f,s), where s-type variable string), counting the value of the counter variable k at each reading step;
  3. Display the value of the counter variable;
  4. Close the file.

program z1;

var k:integer;

s:string;

f:text;

begin

assign(f,"input.pas");

reset(f);

k:=0;

while not eof(f) do begin

readln(f,s); k:=k+1;end;

writeln("k=",k);

close(f);

end.

Task 2: Given a text file. Print all its lines starting with the character "T".

Let’s create an algorithm for solving the problem (Create a text data file – input.pas before starting to solve the problem):

  1. Link logical file f to physical file input.pas;
  2. Open file for reading;
  3. Organize reading data from a file line by line (readln(f,s), where s is a variable of type string), checking at each step whether the line satisfies the condition: the first character is “T”, and if so, display this line on the screen;
  4. Close the file.

program z2;

var k:integer;

s:string;

f:text;

begin

assign(f,"input.pas");

reset(f);

while not eof(f) do begin

readln(f,s);

if s=’T’ then writeln(s);

end;

close(f);

end.

Task 3: Given a text file. Print all its lines containing more than 30 characters.

Let’s create an algorithm for solving the problem (Create a text data file – input.pas before starting to solve the problem):

  1. Link logical file f to physical file input.pas;
  2. Open file for reading;
  3. Organize reading data from a file line by line (readln(f,s), where s is a variable of type string), checking at each step whether the line satisfies the condition: line length is greater than 30, and if so, display this line on the screen;
  4. Close the file.

program z3;

var k:integer;

s:string;

f:text;

begin

assign(f,"input.pas");

reset(f);

while not eof(f) do begin

readln(f,s);

if length(s)>=30 then writeln(s);

end;

close(f);

end.

Task 4: Given a text file. Print all its lines containing the given text as a fragment.

Let’s create an algorithm for solving the problem (Create a text data file – input.pas before starting to solve the problem):

  1. Link logical file f to physical file input.pas;
  2. Open file for reading;
  3. Set a text fragment to search (s1);
  4. Organize reading data from a file line by line (readln(f,s), where s is a variable of type string), checking at each step whether the line satisfies the condition: it contains the specified text (s1) as a fragment, and if so, output this line to the screen;
  5. Close the file.

program z4;

var k:integer;

s1,s:string;

f:text;

begin

writeln('enterfragmenttext’);

readln(s1);

assign(f,"input.pas");

reset(f);

while not eof(f) do begin

readln(f,s);

if pos(s1,s)<>0 then writeln(s);

end;

close(f);

end.

Task 5: Given a text file. Print line 5 to a new text file, and the rest to the screen.

Let’s create an algorithm for solving the problem (Create a text data file – input.pas before starting to solve the problem):

  1. Associate logical file f with physical file input.pas, and logical file g with physical file output.pas;
  2. Open file for reading;
  3. Organize reading data from a file line by line (readln(f,s), where s is a variable of type string), counting the value of the counter variable k at each reading step, and checking at each step whether the counter value is equal to 5, and if so, then output this line to file g, otherwise output it to the screen;
  4. Close the file.

program z5;

var k:integer;

s:string;

f,g:text;

begin

assign(f,"input.pas");

reset(f);

assign(g,"output.pas");

rewrite(g);

k:=0;

while not eof(f) do begin

readln(f,s);k:=k+1;

if k=5 then writeln(g,s);

end;

close(f); close(f);